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

 

Chapter 16 Namespaces

1

extern-alias-directives:

2

extern-alias-directive

3

extern-alias-directives extern-alias-directive

4

extern-alias-directive:

5

extern alias identifier ;

6The scope of an extern-alias-directive extends over the using-directives, global-attributes and namespace-

7member-declarations of its immediately containing compilation-unit or namespace-body. An extern-alias-

8directive contributes its name to the alias declaration space of the containing compilation unit or namespace

9body (§10.3) and not to the declaration space of the containing namespace.

10Within a compilation unit or namespace body that contains an extern-alias-directive, the identifier

11introduced by the extern-alias-directive can be used to reference the aliased namespace. It is a compile-time

12error for the identifier to be the word global.

13Within C# source code, a type is declared to be a member of a single namespace. However, a namespace

14hierarchy referenced by an extern alias may contain types that are also members of other namespaces. For

15example, if A and B are extern aliases, the names A::X, B::C.Y and global::D.Z may, depending on the

16external specification supported by the particular compiler, all refer to the same type.

17The alias introduced by an extern-alias-directive is very similar to the alias introduced by a using-alias-

18directive. See §16.4.1 for more detailed discussion of extern-alias-directives and using-alias-directives.

19Like get and set in property accessors, alias is not a keyword (§9.4.3). The word alias only has special

20meaning when it immediately follows the extern keyword in an extern-alias-directive. [Example: In fact

21an extern alias could use the identifier alias as its name:

22extern alias alias;

23end example]

2416.4 Using directives

25Using-directives facilitate the use of namespaces and types defined in other namespaces. Using-directives

26impact the name resolution process of namespace-or-type-names (§10.8) and simple-names (§14.5.2), but

27unlike declarations, using-directives do not contribute new members to the underlying declaration spaces of

28the compilation units or namespaces within which they are used.

29using-directives:

30

31

32

33

34

using-directive

using-directives using-directive

using-directive: using-alias-directive using-namespace-directive

35A using-alias-directive (§16.4.1) introduces an alias for a namespace or type.

36A using-namespace-directive (§16.4.2) imports the type members of a namespace.

37The scope of a using-directive extends over the namespace-member-declarations of its immediately

38containing compilation unit or namespace body. The scope of a using-directive specifically does not include

39its peer using-directives. Thus, peer using-directives do not affect each other, and the order in which they are

40written is insignificant. In contrast, the scope of an extern-alias-directive includes the using-directives

41defined in the same compilation unit or namespace body.

4216.4.1 Using alias directives

43A using-alias-directive introduces an identifier that serves as an alias for a namespace or type within the

44immediately enclosing compilation unit or namespace body.

245

 

C# LANGUAGE SPECIFICATION

1

using-alias-directive:

2

using identifier = namespace-or-type-name ;

3Within global attributes and member declarations in a compilation unit or namespace body that contains a

4using-alias-directive, the identifier introduced by the using-alias-directive can be used to reference the given

5namespace or type. [Example:

6namespace N1.N2

7{

8class A {}

9}

10namespace N3

11{

12

using A = N1.N2.A;

13class B: A {}

14}

15Above, within member declarations in the N3 namespace, A is an alias for N1.N2.A, and thus class N3.B

16derives from class N1.N2.A. The same effect can be obtained by creating an alias R for N1.N2 and then

17referencing R.A:

18namespace N3

19{

20

using R = N1.N2;

21class B: R.A {}

22}

23end example]

24Within using directives, global attributes and member declarations in a compilation unit or namespace body

25that contains an extern-alias-directive, the identifier introduced by the extern-alias-directive can be used to

26reference the associated namespace. [Example: For example:

27namespace N1

28{

29

extern alias N2;

30class B: N2::A {}

31}

32Above, within member declarations in the N1 namespace, N2 is an alias for some namespace whose

33definition is external to the source code of the program. Class N1.B derives from class N2.A. The same

34effect can be obtained by creating an alias A for N2.A and then referencing A:

35namespace N1

36{

37

extern alias N2;

38

using A = N2::A;

39class B: A {}

40}

41end example]

42An extern-alias-directive or using-alias-directive makes an alias available within a particular compilation

43unit or namespace body, but it does not contribute any new members to the underlying declaration space. In

44other words, an alias directive is not transitive, but, rather, affects only the compilation unit or namespace

45body in which it occurs. [Example: In the following code

46namespace N3

47{

48

extern alias R1;

49using R2 = N1.N2;

50}

246

Chapter 16 Namespaces

1namespace N3

2{

3

class B: R1::A, R2.I {}

// Error, R1 and R2 unknown

4}

5the scopes of the alias directives that introduce R1 and R2 only extend to member declarations in the

6namespace body in which they are contained, so R1 and R2 are unknown in the second namespace

7declaration. However, placing the alias directives in the containing compilation unit causes the alias to

8become available within both namespace declarations:

9extern alias R1;

10using R2 = N1.N2;

11namespace N3

12{

13class B: R1::A, R2.I {}

14}

15namespace N3

16{

17class C: R1::A, R2.I {}

18}

19end example]

20Each extern-alias-directive or using-alias-directive in a compilation-unit or namespace-body contributes a

21name to the alias declaration space (§10.3) of the immediately enclosing compilation-unit or namespace-

22body. The identifier of the alias directive shall be unique within the corresponding alias declaration space.

23The alias identifier need not be unique within the global declaration space or the declaration space of the

24corresponding namespace. [Example:

25extern alias A;

26extern alias B;

27

using

A

= N1.N2;

//

Error: alias A already exists

28

class

B

{}

//

Ok

29The using alias named A causes an error since there is already an alias named A in the same compilation unit.

30The class named B does not conflict with the extern alias named B since these names are added to distinct

31declaration spaces. The former is added to the global declaration space and the latter is added to the alias

32declaration space for this compilation unit.

33When an alias name matches the name of a member of a namespace, usage of either must be appropriately

34qualified:

35namespace N1.N2

36{

37class B {}

38}

39namespace N3

40{

41

class A {}

42class B : A {}

43}

44namespace N3

45{

46

using A = N1.N2;

 

47

using B = N1.N2.B;

 

48

class W : B {}

// Error: B is ambiguous

49

class X : A.B {}

// Error: A is ambiguous

50

class Y : A::B {}

// Ok: uses N1.N2.B

51

class Z : N3.B {}

// Ok: uses N3.B

52}

53In the second namespace body for N3, unqualified use of B results in an error, since N3 contains a member

54named B and the namespace body also declares an alias with name B. Similarly for A. The class N3.B can be

55referenced as N3.B or global::N3.B. The alias A can be used in a qualified-alias-member (§16.7), such as

247

C# LANGUAGE SPECIFICATION

1A::B. The alias B is essentially useless. It cannot be used in a qualified-alias-member since only namespace

2aliases can be used in a qualified-alias-member and B aliases a type. end example]

3Just like regular members, names introduced by alias directives are hidden by similarly named members in

4nested scopes. [Example: In the following code

5using R = N1.N2;

6namespace N3

7{

8

class

R {}

 

9

class

B: R.A {}

// Error, R has no member A

10}

11the reference to R.A in the declaration of B causes a compile-time error because R refers to N3.R, not

12N1.N2. end example]

13The order in which extern-alias-directives are written has no significance. Likewise, the order in which

14using-alias-directives are written has no significance, but all using-alias-directives must come after all

15extern-alias-directives in the same compilation unit or namespace body. Resolution of the namespace-or-

16type-name referenced by a using-alias-directive is not affected by the using-alias-directive itself or by other

17using-directives in the immediately containing compilation unit or namespace body, but may be affected by

18extern-alias-directives in the immediately containing compilation unit or namespace body. In other words,

19the namespace-or-type-name of a using-alias-directive is resolved as if the immediately containing

20compilation unit or namespace body had no using-directives but has the correct set of extern-alias-

21directives. [Example: In the following code

22namespace N1.N2 {}

23namespace N3

24{

25

extern alias E;

 

26

using

R1

= E::N;

// OK

27

using

R2

= N1;

// OK

28

using

R3

= N1.N2;

// OK

29

using

R4

= R2.N2;

// Error, R2 unknown

30}

31the last using-alias-directive results in a compile-time error because it is not affected by the previous using-

32alias-directive. The first using-alias-directive does not result in an error since the scope of the extern alias E

33includes the using-alias-directive. end example]

34A using-alias-directive can create an alias for any namespace or type, including the namespace within which

35it appears and any namespace or type nested within that namespace.

36Accessing a namespace or type through an alias yields exactly the same result as accessing that namespace

37or type through its declared name. [Example: Given

38namespace N1.N2

39{

40class A {}

41}

42namespace N3

43{

44

using R1 =

N1;

 

45

using R2 = N1.N2;

 

46

class B

 

 

47

{

 

 

48

N1.N2.A a;

// refers to N1.N2.A

49

R1.N2.A b;

// refers to N1.N2.A

50

R2.A c;

 

// refers to N1.N2.A

51}

52}

248

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