Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
14
Добавлен:
23.08.2013
Размер:
2.55 Mб
Скачать

C# LANGUAGE SPECIFICATION

1were to extend only from its declaration to the end of the block, then in the example above, the first

2assignment would assign to the instance variable and the second assignment would assign to the local

3variable. (In certain situations, but not in the example above, this could lead to a compile-time error if the

4statements of the block were later to be rearranged.)

5The meaning of a name within a block can differ based on the context in which the name is used. In the

6example

7using System;

8class A {}

9class Test

10{

11

static void Main() {

 

12

string A = "hello, world";

// declarator context

13

string s = A;

// expression context

14

Type t = typeof(A);

// type context

15

Console.WriteLine(s);

// writes "hello, world"

16

Console.WriteLine(t.ToString());

// writes "Type: A"

17}

18}

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

20the class A. end note]

2110.7.1 Name hiding

22The scope of an entity typically encompasses more program text than the declaration space of the entity. In

23particular, the scope of an entity can include declarations that introduce new declaration spaces containing

24entities of the same name. Such declarations cause the original entity to become hidden. Conversely, an

25entity is said to be visible when it is not hidden.

26Name hiding occurs when scopes overlap through nesting and when scopes overlap through inheritance. The

27characteristics of the two types of hiding are described in the following subclauses.

28Local variables, local constants, parameters, and method type parameters cannot hide other local variables,

29local constants, parameters, or method type parameters (§10.3).

3010.7.1.1 Hiding through nesting

31Name hiding through nesting can occur as a result of nesting namespaces or types within namespaces, as a

32result of nesting types within classes or structs, and as a result of parameter, local variable, and local

33constant declarations. [Example: In the following code

34class A

35{

36

int i = 0;

37

void F() {

38

int i = 1;

39

}

40

void G() {

41

i = 1;

42}

43}

44within the F method, the instance variable i is hidden by the local variable i, but within the G method, i still

45refers to the instance variable. end example]

46When a name in an inner scope hides a name in an outer scope, it hides all overloaded occurrences of that

47name. [Example: In the following code

48class Outer

49{

50

static void F(int i) {}

96

Chapter 10 Basic concepts

1

static void F(string s) {}

2

class Inner

 

3

{

 

4

void G() {

 

5

F(1);

// Invokes Outer.Inner.F

6

F("Hello");

// Error

7

}

 

8

static void F(long l) {}

9}

10}

11the call F(1) invokes the F declared in Inner because all outer occurrences of F are hidden by the inner

12declaration. For the same reason, the call F("Hello") results in a compile-time error. end example]

1310.7.1.2 Hiding through inheritance

14Name hiding through inheritance occurs when classes or structs redeclare names that were inherited from

15base classes. This type of name hiding takes one of the following forms:

16A constant, field, property, or event introduced in a class or struct hides all base class members with the

17same name and no type parameters.

18A type introduced in a class or struct hides all base class members with the same name and same number

19of type parameters.

20A method introduced in a class or struct hides all non-method base class members with the same name

21and either the same number of type parameters or no type parameters, and all base class methods with

22the same signature (method name, type parameter count, parameter count, modifiers, and types).

23An indexer introduced in a class or struct hides all base class indexers with the same signature

24(parameter count and types).

25Contrary to hiding a name from an outer scope, hiding an accessible name from an inherited scope causes a

26warning to be reported. [Example: In the following code

27class Base

28{

29public void F() {}

30}

31class Derived: Base

32{

33

public void F() {}

// Warning, hiding an inherited name

34}

35the declaration of F in Derived causes a warning to be reported. Hiding an inherited name is specifically

36not an error, since that would preclude separate evolution of base classes. For example, the above situation

37might have come about because a later version of Base introduced an F method that wasn’t present in an

38earlier version of the class. Had the above situation been an error, then any change made to a base class in a

39separately versioned class library could potentially cause derived classes to become invalid. end example]

40The warning caused by hiding an inherited name can be eliminated through use of the new modifier:

41[Example:

42class Base

43{

44public void F() {}

45}

46class Derived: Base

47{

48new public void F() {}

49}

50The new modifier indicates that the F in Derived is “new”, and that it is indeed intended to hide the

51inherited member. end example]

97

Соседние файлы в предмете Электротехника