X hits on this document





79 / 396


Type t = typeof(A);// type context

Console.WriteLine(s);// writes "hello, world" Console.WriteLine(t);// writes "A" } }

the name A is used in an expression context to refer to the local variable A and in a type context to refer to the class A.

3.7.1 Name hiding

The scope of an entity typically encompasses more program text than the declaration space of the entity. In particular, the scope of an entity may include declarations that introduce new declaration spaces containing entities of the same name. Such declarations cause the original entity to become hidden. Conversely, an entity is said to be visible when it is not hidden.

Name hiding occurs when scopes overlap through nesting and when scopes overlap through inheritance. The characteristics of the two types of hiding are described in the following sections. Hiding through nesting

Name hiding through nesting can occur as a result of nesting namespaces or types within namespaces, as a result of nesting types within classes or structs, and as a result of parameter and local variable declarations.

In the example

class A { int i = 0;

void F() { int i = 1; }

void G() { i = 1; } }

within the F method, the instance variable i is hidden by the local variable i, but within the G method, i still refers to the instance variable.

When a name in an inner scope hides a name in an outer scope, it hides all overloaded occurrences of that name. In the example

class Outer { static void F(int i) {}

static void F(string s) {}

class Inner { void G() { F(1);// Invokes Outer.Inner.F F("Hello");// Error }

static void F(long l) {} } }

66Copyright Microsoft Corporation 1999-2003. All Rights Reserved.

Document info
Document views487
Page views487
Page last viewedSun Oct 23 21:20:11 UTC 2016