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

Chapter 16 Namespaces

1The namespace-name referenced by a using-namespace-directive is resolved in the same way as the

2namespace-or-type-name referenced by a using-alias-directive. Thus, using-namespace-directives in the

3same compilation unit or namespace body do not affect each other and can be written in any order.

416.5 Namespace members

5A namespace-member-declaration is either a namespace-declaration (§16.2) or a type-declaration (§16.6).

6namespace-member-declarations:

7

8

9

10

11

namespace-member-declaration

namespace-member-declarations namespace-member-declaration

namespace-member-declaration: namespace-declaration type-declaration

12A compilation unit or a namespace body can contain namespace-member-declarations, and such

13declarations contribute new members to the underlying declaration space of the containing compilation unit

14or namespace body.

1516.6 Type declarations

16A type-declaration is a class-declaration (§17.1), a struct-declaration (§18.1), an interface-declaration

17(§20.1), an enum-declaration (§21.1), or a delegate-declaration (§22.1).

18type-declaration:

19

20

21

22

23

class-declaration struct-declaration interface-declaration enum-declaration delegate-declaration

24A type-declaration can occur as a top-level declaration in a compilation unit or as a member declaration

25within a namespace, class, or struct.

26When a type declaration for a type T occurs as a top-level declaration in a compilation unit, the fully

27qualified name (§10.8.2) of the type declaration is the same as the unqualified name of the declaration

28(§10.8.1). When a type declaration for a type T occurs within a namespace, class, or struct, the fully

29qualified name (§10.8.2) of the type declaration is S.N, where S is the fully qualified name of the containing

30namespace or type declaration, and N is the unqualified name of the declaration (§10.8.1).

31A type declared within a class or struct is called a nested type (§17.2.6).

32The permitted access modifiers and the default access for a type declaration depend on the context in which

33the declaration takes place (§10.5.1):

34Types declared in compilation units or namespace declarations can have public or internal access.

35The default is internal access.

36Types declared in classes can have public, protected internal, protected, internal, or

37private access. The default is private access.

38Types declared in structs can have public, internal, or private access. The default is private

39access.

4016.7 Qualified alias member

41A qualified-alias-member provides explicit access to the global namespace and to extern or using aliases

42that are potentially hidden by other entities.

43qualified-alias-member:

44

identifier :: identifier type-argument-listopt

251

C# LANGUAGE SPECIFICATION

1A qualified-alias-member can be used as a namespace-or-type-name (§10.8) or as the left operand in a

2member-access (§14.5.4).

3A qualified-alias-member consists of two identifiers, referred to as the left-hand and right-hand identifiers,

4seperated by the :: token and optionally followed by a type-argument-list. When the left-hand identifier is

5global then the global namespace is searched for the right-hand identifier. For any other left-hand

6identifier, that identifier is looked up as an extern or using alias (§16.3 and §16.4.1). A compile-time error

7occurs if there is no such alias or the alias references a type. If the alias references a namespace then that

8namespace is searched for the right-hand identifier.

9

10A qualified-alias-member has one of two forms:

11A::B<G1, ..., GN>, where A and B represent identifiers, and <G1, ..., GN> is a type argument list. (N is

12always at least one.)

13A::B, where A and B represent identifiers. (In this case, N is considered to be zero.)

14Using this notation, the meaning of a qualified-alias-member is determined as follows:

15If A is the identifier global, then the global namespace is searched for B:

16o If the global namespace contains a namespace named B and N is zero, then the qualified-alias-

17member refers to that namespace.

18o Otherwise, if the global namespace contains a non-generic type named B and N is zero, then the

19qualified-alias-member refers to that type.

20o Otherwise, if the global namespace contains a type named B that has N type parameters, then the

21qualified-alias-member refers to that type constructed with the given type arguments.

22o Otherwise, the qualified-alias-member is undefined and a compile-time error occurs.

23Otherwise, starting with the namespace declaration (§16.2) immediately containing the qualified-alias-

24member (if any), continuing with each enclosing namespace declaration (if any), and ending with the

25compilation unit containing the qualified-alias-member, the following steps are evaluated until an entity

26is located:

27o If the namespace declaration or compilation unit contains a using-alias-directive that associates A

28with a type, then the qualified-alias-member is undefined and a compile-time error occurs.

29o Otherwise, if the namespace declaration or compilation unit contains an extern-alias-directive or

30using-alias-directive that associates A with a namespace, then:

31If the namespace associated with A contains a namespace named B and N is zero, then the

32

qualified-alias-member refers to that namespace.

33

Otherwise, if the namespace associated with A contains a non-generic type named B and N is

34

zero, then the qualified-alias-member refers to that type.

35

Otherwise, if the namespace associated with A contains a type named B that has N type

36

parameters, then the qualified-alias-member refers to that type constructed with the given type

37

arguments.

38Otherwise, the qualified-alias-member is undefined and a compile-time error occurs.

39Otherwise, the qualified-alias-member is undefined and a compile-time error occurs.

40[Example: In the code:

41using S = System.Net.Sockets;

42class A {

43public static int x;

44}

252

 

Chapter 16 Namespaces

1

class C {

2

public void F(int A, object S) {

3

// Use global::A.x instead of A.x

4

global::A.x += A;

5

// Use S::Socket instead of S.Socket

6

S::Socket s = S as S::Socket;

7}

8}

9the class A is referenced with global::A and the type System.Net.Sockets.Socket is referenced with

10S::Socket. Using A.x and S.Socket instead would have caused compile-time errors because A and S

11would have resolved to the parameters. end example]

12[Note: The identifier global has special meaning only when used as the left-hand identifier of a qualified-

13alias-name. It is not a keyword and it is not itself an alias. In the code:

14class A { }

15class C {

16

global.A x; // Error: global is not defined

17global::A y; // Valid: References A in the global namespace

18}

19using global.A causes a compile-time error since there is no entity named global in scope. If some entity

20named global were in scope, then global in global.A would have resolved to that entity.

21Using global as the left-hand identifier of a qualified-alias-member always causes a lookup in the global

22namespace, even if there is a using alias named global. [Example: In the code:

23using global = MyGlobalTypes;

24class A { }

25class C {

26

global.A x; // Valid: References MyGlobalTypes.A

27global::A y; // Valid: References A in the global namespace

28}

29global.A resolves to MyGlobalTypes.A and global::A resolves to class A in the global namespace.

30end note]

253

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