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

Chapter 16 Namespaces

1the names N1.N2.A, R1.N2.A, and R2.A are equivalent and all refer to the class whose fully qualified

2name is N1.N2.A. end example]

3Although each part of a partial type (§17.1.4) is declared within the same namespace, the parts are typically

4written within different namespace declarations. Thus, different extern alias directives and using

5directives can be present for each part. When interpreting simple names (§14.5.2) within one part, only the

6extern alias directives and using directives of the namespace bodies and compilation unit enclosing that

7part are considered. This may result in the same identifier having different meanings in different parts.

8[Example:

9namespace N

10{

11

using List = System.Collections.ArrayList;

12

partial class A

 

13

{

 

14

List x;

// x has type System.Collections.ArrayList

15}

16}

17namespace N

18{

19

using List = Widgets.LinkedList;

20

partial class A

 

21

{

 

22

List y;

// y has type Widgets.LinkedList

23}

24}

25end example]

2616.4.2 Using namespace directives

27A using-namespace-directive imports the types contained in a namespace into the immediately enclosing

28compilation unit or namespace body, enabling the identifier of each type to be used without qualification.

29using-namespace-directive:

30using namespace-name ;

31Within member declarations in a compilation unit or namespace body that contains a using-namespace-

32directive, the types contained in the given namespace can be referenced directly. [Example:

33namespace N1.N2

34{

35class A {}

36}

37namespace N3

38{

39

using N1.N2;

40class B: A {}

41}

42Above, within member declarations in the N3 namespace, the type members of N1.N2 are directly

43available, and thus class N3.B derives from class N1.N2.A. end example]

44A using-namespace-directive imports the types contained in the given namespace, but specifically does not

45import nested namespaces. [Example: In the following code

46namespace N1.N2

47{

48class A {}

49}

50namespace N3

51{

52

using N1;

249

C# LANGUAGE SPECIFICATION

1

class B: N2.A {}

// Error, N2 unknown

2}

3the using-namespace-directive imports the types contained in N1, but not the namespaces nested in N1. Thus,

4the reference to N2.A in the declaration of B results in a compile-time error because no members named N2

5are in scope. end example]

6Unlike a using-alias-directive, a using-namespace-directive can import types whose identifiers are already

7defined within the enclosing compilation unit or namespace body. In effect, names imported by a using-

8namespace-directive are hidden by similarly named members in the enclosing compilation unit or

9namespace body. [Example:

10namespace N1.N2

11{

12

class A {}

13class B {}

14}

15namespace N3

16{

17

using N1.N2;

18class A {}

19}

20Here, within member declarations in the N3 namespace, A refers to N3.A rather than N1.N2.A. end example]

21When more than one namespace imported by using-namespace-directives in the same compilation unit or

22namespace body contain types by the same name, references to that name are considered ambiguous.

23[Example: In the following code

24namespace N1

25{

26class A {}

27}

28namespace N2

29{

30class A {}

31}

32namespace N3

33{

34

using N1;

 

35

using N2;

 

36

class B: A {}

// Error, A is ambiguous

37}

38both N1 and N2 contain a member A, and because N3 imports both, referencing A in N3 is a compile-time

39error. In this situation, the conflict can be resolved either through qualification of references to A, or by

40introducing a using-alias-directive that picks a particular A. For example:

41namespace N3

42{

43

using N1;

 

44

using N2;

 

45

using A = N1.A;

 

46

class B: A {}

// A means N1.A

47}

48end example]

49Like a using-alias-directive, a using-namespace-directive does not contribute any new members to the

50underlying declaration space of the namespace, but, rather, affects only the compilation unit or namespace

51body in which it appears.

250

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