- •Foreword
- •Introduction
- •Scope
- •Conformance
- •Normative references
- •Definitions
- •Notational conventions
- •Acronyms and abbreviations
- •General description
- •Language overview
- •Getting started
- •Types
- •Predefined types
- •Conversions
- •Array types
- •Type system unification
- •Variables and parameters
- •Automatic memory management
- •Expressions
- •Statements
- •Classes
- •Constants
- •Fields
- •Methods
- •Properties
- •Events
- •Operators
- •Indexers
- •Instance constructors
- •Destructors
- •Static constructors
- •Inheritance
- •Static classes
- •Partial type declarations
- •Structs
- •Interfaces
- •Delegates
- •Enums
- •Namespaces and assemblies
- •Versioning
- •Extern Aliases
- •Attributes
- •Generics
- •Why generics?
- •Creating and consuming generics
- •Multiple type parameters
- •Constraints
- •Generic methods
- •Anonymous methods
- •Iterators
- •Lexical structure
- •Programs
- •Grammars
- •Lexical grammar
- •Syntactic grammar
- •Grammar ambiguities
- •Lexical analysis
- •Line terminators
- •Comments
- •White space
- •Tokens
- •Unicode escape sequences
- •Identifiers
- •Keywords
- •Literals
- •Boolean literals
- •Integer literals
- •Real literals
- •Character literals
- •String literals
- •The null literal
- •Operators and punctuators
- •Pre-processing directives
- •Conditional compilation symbols
- •Pre-processing expressions
- •Declaration directives
- •Conditional compilation directives
- •Diagnostic directives
- •Region control
- •Line directives
- •Pragma directives
- •Basic concepts
- •Application startup
- •Application termination
- •Declarations
- •Members
- •Namespace members
- •Struct members
- •Enumeration members
- •Class members
- •Interface members
- •Array members
- •Delegate members
- •Member access
- •Declared accessibility
- •Accessibility domains
- •Protected access for instance members
- •Accessibility constraints
- •Signatures and overloading
- •Scopes
- •Name hiding
- •Hiding through nesting
- •Hiding through inheritance
- •Namespace and type names
- •Unqualified name
- •Fully qualified names
- •Automatic memory management
- •Execution order
- •Types
- •Value types
- •The System.ValueType type
- •Default constructors
- •Struct types
- •Simple types
- •Integral types
- •Floating point types
- •The decimal type
- •The bool type
- •Enumeration types
- •Reference types
- •Class types
- •The object type
- •The string type
- •Interface types
- •Array types
- •Delegate types
- •Boxing and unboxing
- •Boxing conversions
- •Unboxing conversions
- •Variables
- •Variable categories
- •Static variables
- •Instance variables
- •Instance variables in classes
- •Instance variables in structs
- •Array elements
- •Value parameters
- •Reference parameters
- •Output parameters
- •Local variables
- •Default values
- •Definite assignment
- •Initially assigned variables
- •Initially unassigned variables
- •Precise rules for determining definite assignment
- •General rules for statements
- •Block statements, checked, and unchecked statements
- •Expression statements
- •Declaration statements
- •If statements
- •Switch statements
- •While statements
- •Do statements
- •For statements
- •Break, continue, and goto statements
- •Throw statements
- •Return statements
- •Try-catch statements
- •Try-finally statements
- •Try-catch-finally statements
- •Foreach statements
- •Using statements
- •Lock statements
- •General rules for simple expressions
- •General rules for expressions with embedded expressions
- •Invocation expressions and object creation expressions
- •Simple assignment expressions
- •&& expressions
- •|| expressions
- •! expressions
- •?: expressions
- •Anonymous method expressions
- •Yield statements
- •Variable references
- •Atomicity of variable references
- •Conversions
- •Implicit conversions
- •Identity conversion
- •Implicit numeric conversions
- •Implicit enumeration conversions
- •Implicit reference conversions
- •Boxing conversions
- •Implicit type parameter conversions
- •Implicit constant expression conversions
- •User-defined implicit conversions
- •Explicit conversions
- •Explicit numeric conversions
- •Explicit enumeration conversions
- •Explicit reference conversions
- •Unboxing conversions
- •User-defined explicit conversions
- •Standard conversions
- •Standard implicit conversions
- •Standard explicit conversions
- •User-defined conversions
- •Permitted user-defined conversions
- •Evaluation of user-defined conversions
- •User-defined implicit conversions
- •User-defined explicit conversions
- •Anonymous method conversions
- •Method group conversions
- •Expressions
- •Expression classifications
- •Values of expressions
- •Operators
- •Operator precedence and associativity
- •Operator overloading
- •Unary operator overload resolution
- •Binary operator overload resolution
- •Candidate user-defined operators
- •Numeric promotions
- •Unary numeric promotions
- •Binary numeric promotions
- •Member lookup
- •Base types
- •Function members
- •Argument lists
- •Overload resolution
- •Applicable function member
- •Better function member
- •Better conversion
- •Function member invocation
- •Invocations on boxed instances
- •Primary expressions
- •Literals
- •Simple names
- •Invariant meaning in blocks
- •Parenthesized expressions
- •Member access
- •Identical simple names and type names
- •Invocation expressions
- •Method invocations
- •Delegate invocations
- •Element access
- •Array access
- •Indexer access
- •This access
- •Base access
- •Postfix increment and decrement operators
- •The new operator
- •Object creation expressions
- •Array creation expressions
- •Delegate creation expressions
- •The typeof operator
- •The checked and unchecked operators
- •Default value expression
- •Anonymous methods
- •Anonymous method signatures
- •Anonymous method blocks
- •Outer variables
- •Captured outer variables
- •Instantiation of local variables
- •Anonymous method evaluation
- •Implementation example
- •Unary expressions
- •Unary plus operator
- •Unary minus operator
- •Logical negation operator
- •Bitwise complement operator
- •Prefix increment and decrement operators
- •Cast expressions
- •Arithmetic operators
- •Multiplication operator
- •Division operator
- •Remainder operator
- •Addition operator
- •Subtraction operator
- •Shift operators
- •Relational and type-testing operators
- •Integer comparison operators
- •Floating-point comparison operators
- •Decimal comparison operators
- •Boolean equality operators
- •Enumeration comparison operators
- •Reference type equality operators
- •String equality operators
- •Delegate equality operators
- •The is operator
- •The as operator
- •Logical operators
- •Integer logical operators
- •Enumeration logical operators
- •Boolean logical operators
- •Conditional logical operators
- •Boolean conditional logical operators
- •User-defined conditional logical operators
- •Conditional operator
- •Assignment operators
- •Simple assignment
- •Compound assignment
- •Event assignment
- •Expression
- •Constant expressions
- •Boolean expressions
- •Statements
- •End points and reachability
- •Blocks
- •Statement lists
- •The empty statement
- •Labeled statements
- •Declaration statements
- •Local variable declarations
- •Local constant declarations
- •Expression statements
- •Selection statements
- •The if statement
- •The switch statement
- •Iteration statements
- •The while statement
- •The do statement
- •The for statement
- •The foreach statement
- •Jump statements
- •The break statement
- •The continue statement
- •The goto statement
- •The return statement
- •The throw statement
- •The try statement
- •The checked and unchecked statements
- •The lock statement
- •The using statement
- •The yield statement
- •Namespaces
- •Compilation units
- •Namespace declarations
- •Extern alias directives
- •Using directives
- •Using alias directives
- •Using namespace directives
- •Namespace members
- •Type declarations
- •Qualified alias member
- •Classes
- •Class declarations
- •Class modifiers
- •Abstract classes
- •Sealed classes
- •Static classes
- •Class base specification
- •Base classes
- •Interface implementations
- •Class body
- •Partial declarations
- •Class members
- •Inheritance
- •The new modifier
- •Access modifiers
- •Constituent types
- •Static and instance members
- •Nested types
- •Fully qualified name
- •Declared accessibility
- •Hiding
- •this access
- •Reserved member names
- •Member names reserved for properties
- •Member names reserved for events
- •Member names reserved for indexers
- •Member names reserved for destructors
- •Constants
- •Fields
- •Static and instance fields
- •Readonly fields
- •Using static readonly fields for constants
- •Versioning of constants and static readonly fields
- •Volatile fields
- •Field initialization
- •Variable initializers
- •Static field initialization
- •Instance field initialization
- •Methods
- •Method parameters
- •Value parameters
- •Reference parameters
- •Output parameters
- •Parameter arrays
- •Static and instance methods
- •Virtual methods
- •Override methods
- •Sealed methods
- •Abstract methods
- •External methods
- •Method body
- •Method overloading
- •Properties
- •Static and instance properties
- •Accessors
- •Virtual, sealed, override, and abstract accessors
- •Events
- •Field-like events
- •Event accessors
- •Static and instance events
- •Virtual, sealed, override, and abstract accessors
- •Indexers
- •Indexer overloading
- •Operators
- •Unary operators
- •Binary operators
- •Conversion operators
- •Instance constructors
- •Constructor initializers
- •Instance variable initializers
- •Constructor execution
- •Default constructors
- •Private constructors
- •Optional instance constructor parameters
- •Static constructors
- •Destructors
- •Structs
- •Struct declarations
- •Struct modifiers
- •Struct interfaces
- •Struct body
- •Struct members
- •Class and struct differences
- •Value semantics
- •Inheritance
- •Assignment
- •Default values
- •Boxing and unboxing
- •Meaning of this
- •Field initializers
- •Constructors
- •Destructors
- •Static constructors
- •Struct examples
- •Database integer type
- •Database boolean type
- •Arrays
- •Array types
- •The System.Array type
- •Array creation
- •Array element access
- •Array members
- •Array covariance
- •Arrays and the generic IList interface
- •Array initializers
- •Interfaces
- •Interface declarations
- •Interface modifiers
- •Base interfaces
- •Interface body
- •Interface members
- •Interface methods
- •Interface properties
- •Interface events
- •Interface indexers
- •Interface member access
- •Fully qualified interface member names
- •Interface implementations
- •Explicit interface member implementations
- •Interface mapping
- •Interface implementation inheritance
- •Interface re-implementation
- •Abstract classes and interfaces
- •Enums
- •Enum declarations
- •Enum modifiers
- •Enum members
- •The System.Enum type
- •Enum values and operations
- •Delegates
- •Delegate declarations
- •Delegate instantiation
- •Delegate invocation
- •Exceptions
- •Causes of exceptions
- •The System.Exception class
- •How exceptions are handled
- •Common Exception Classes
- •Attributes
- •Attribute classes
- •Attribute usage
- •Positional and named parameters
- •Attribute parameter types
- •Attribute specification
- •Attribute instances
- •Compilation of an attribute
- •Run-time retrieval of an attribute instance
- •Reserved attributes
- •The AttributeUsage attribute
- •The Conditional attribute
- •Conditional Methods
- •Conditional Attribute Classes
- •The Obsolete attribute
- •Unsafe code
- •Unsafe contexts
- •Pointer types
- •Fixed and moveable variables
- •Pointer conversions
- •Pointers in expressions
- •Pointer indirection
- •Pointer member access
- •Pointer element access
- •The address-of operator
- •Pointer increment and decrement
- •Pointer arithmetic
- •Pointer comparison
- •The sizeof operator
- •The fixed statement
- •Stack allocation
- •Dynamic memory allocation
- •Generics
- •Generic class declarations
- •Type parameters
- •The instance type
- •Members of generic classes
- •Static fields in generic classes
- •Static constructors in generic classes
- •Accessing protected members
- •Overloading in generic classes
- •Parameter array methods and type parameters
- •Overriding and generic classes
- •Operators in generic classes
- •Nested types in generic classes
- •Generic struct declarations
- •Generic interface declarations
- •Uniqueness of implemented interfaces
- •Explicit interface member implementations
- •Generic delegate declarations
- •Constructed types
- •Type arguments
- •Open and closed types
- •Base classes and interfaces of a constructed type
- •Members of a constructed type
- •Accessibility of a constructed type
- •Conversions
- •Using alias directives
- •Generic methods
- •Generic method signatures
- •Virtual generic methods
- •Calling generic methods
- •Inference of type arguments
- •Using a generic method with a delegate
- •Constraints
- •Satisfying constraints
- •Member lookup on type parameters
- •Type parameters and boxing
- •Conversions involving type parameters
- •Iterators
- •Iterator blocks
- •Enumerator interfaces
- •Enumerable interfaces
- •Yield type
- •This access
- •Enumerator objects
- •The MoveNext method
- •The Current property
- •The Dispose method
- •Enumerable objects
- •The GetEnumerator method
- •Implementation example
- •Lexical grammar
- •Line terminators
- •White space
- •Comments
- •Unicode character escape sequences
- •Identifiers
- •Keywords
- •Literals
- •Operators and punctuators
- •Pre-processing directives
- •Syntactic grammar
- •Basic concepts
- •Types
- •Expressions
- •Statements
- •Classes
- •Structs
- •Arrays
- •Interfaces
- •Enums
- •Delegates
- •Attributes
- •Generics
- •Grammar extensions for unsafe code
- •Undefined behavior
- •Implementation-defined behavior
- •Unspecified behavior
- •Other Issues
- •Capitalization styles
- •Pascal casing
- •Camel casing
- •All uppercase
- •Capitalization summary
- •Word choice
- •Namespaces
- •Classes
- •Interfaces
- •Enums
- •Static fields
- •Parameters
- •Methods
- •Properties
- •Events
- •Case sensitivity
- •Avoiding type name confusion
- •Documentation Comments
- •Introduction
- •Recommended tags
- •<code>
- •<example>
- •<exception>
- •<list>
- •<para>
- •<param>
- •<paramref>
- •<permission>
- •<remarks>
- •<returns>
- •<seealso>
- •<summary>
- •<value>
- •Processing the documentation file
- •ID string format
- •ID string examples
- •An example
- •C# source code
- •Resulting XML
|
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
