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

Chapter 14 Expressions

114.5.1 Literals

2A primary-expression that consists of a literal (§9.4.4) is classified as a value.

314.5.2 Simple names

4A simple-name consists of an identifier, optionally followed by a type argument list:

5simple-name:

6

identifier type-argument-listopt

7A simple-name is either of the form I or of the form I<A1, ..., AK>, where I is a single identifier and <A1,

8..., AK> is an optional type-argument-list. When no type-argument-list is specified, consider K to be zero.

9The simple-name is evaluated and classified as follows:

10If K is zero and the simple-name appears within a block and if the block’s (or an enclosing block’s) local

11variable declaration space (§10.3) contains a local variable, parameter or constant with name I, then the

12simple-name refers to that local variable, parameter or constant and is classified as a variable or value.

13If K is zero and the simple-name appears within the body of a generic method declaration and if that

14declaration includes a type parameter with name I, then the simple-name refers to that type parameter.

15Otherwise, for each instance type T (§26.1.2), starting with the instance type of the immediately

16enclosing type declaration and continuing with the instance type of each enclosing class or struct

17declaration (if any):

18o If K is zero and the declaration of T includes a type parameter with name I, then the simple-name

19refers to that type parameter.

20o Otherwise, if a member lookup (§14.3) of I in T with K type arguments produces a match:

21If T is the instance type of the immediately enclosing class or struct type and the lookup

22

identifies one or more methods, the result is a method group with an associated instance

23

expression of this. If a type argument list was specified, it is used in calling a generic method

24

(§26.6.3).

25

Otherwise, if T is the instance type of the immediately enclosing class or struct type, if the

26

lookup identifies an instance member, and if the reference occurs within the block of an instance

27

constructor, an instance method, or an instance accessor, the result is the same as a member

28

access (§14.5.4) of the form this.I. This can only happen when K is zero.

29

Otherwise, the result is the same as a member access (§14.5.4) of the form T.I or T.I<A1, ...,

30AK>. In this case, it is a compile-time error for the simple-name to refer to an instance member.

31Otherwise, for each namespace N, starting with the namespace in which the simple-name occurs,

32continuing with each enclosing namespace (if any), and ending with the global namespace, the following

33steps are evaluated until an entity is located:

34o If K is zero and I is the name of a namespace in N, then:

35If the location where the simple-name occurs is enclosed by a namespace declaration for N and

36

the namespace declaration contains an extern-alias-directive or using-alias-directive that

37

associates the name I with a namespace or type, then the simple-name is ambiguous and a

38

compile-time error occurs.

39Otherwise, the simple-name refers to the namespace named I in N.

40o Otherwise, if N contains an accessible type having name I and K type parameters, then:

41If K is zero and the location where the simple-name occurs is enclosed by a namespace

42

declaration for N and the namespace declaration contains an extern-alias-directive or using-

43

alias-directive that associates the name I with a namespace or type, then the simple-name is

44

ambiguous and a compile-time error occurs.

161

C# LANGUAGE SPECIFICATION

1

2

Otherwise, the namespace-or-type-name refers to the type constructed with the given type arguments.

3o Otherwise, if the location where the simple-name occurs is enclosed by a namespace declaration

4for N:

5If K is zero and the namespace declaration contains an extern-alias-directive or using-alias-

6

directive that associates the name I with an imported namespace or type, then the simple-name

7

refers to that namespace or type.

8

Otherwise, if the namespaces imported by the using-namespace-directives of the namespace

9

declaration contain exactly one type having name I and K type parameters, then the simple-

10

name refers to that type constructed with the given type arguments.

11

Otherwise, if the namespaces imported by the using-namespace-directives of the namespace

12

declaration contain more than one type having name I and K type parameters, then the simple-

13

name is ambiguous and an error occurs.

14[Note: This entire step is exactly parallel to the corresponding step in the processing of a namespace-or-

15type-name (§10.8). end note]

16Otherwise, the simple-name is undefined and a compile-time error occurs.

1714.5.2.1 Invariant meaning in blocks

18For each occurrence of a given identifier as a simple-name in an expression or declarator, every other

19occurrence of the same identifier as a simple-name in an expression or declarator within the immediately

20enclosing block (§15.2) or switch-block (§15.7.2) shall refer to the same entity. [Note: This rule ensures that

21the meaning of a name is always the same within a block. end note]

22[Example: The example

23class Test

24{

25

double x;

26

void F(bool b) {

27

x = 1.0;

28

if (b) {

29

int x = 1;

30

}

31}

32}

33results in a compile-time error because x refers to different entities within the outer block (the extent of

34which includes the nested block in the if statement). In contrast, the example

35class Test

36{

37

double x;

38

void F(bool b) {

39

if (b) {

40

x = 1.0;

41

}

42

else {

43

int x = 1;

44

}

45}

46}

47is permitted because the name x is never used in the outer block. end example]

48[Note: The rule of invariant meaning applies only to simple names. It is perfectly valid for the same

49identifier to have one meaning as a simple name and another meaning as right operand of a member access

50(§14.5.4). end note] [Example:

162

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