ECMA-334: 10.8 Namespace and type names
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| C# Language Specification |
|
| © 2006 ECMA International |
10.8 Namespace and type names
Several contexts in a C# program require a namespace-name or a type-name to be specified.
- namespace-name:
- namespace-or-type-name
- type-name:
- namespace-or-type-name
- namespace-or-type-name:
- identifier type-argument-listopt
- qualified-alias-member
- namespace-or-type-name
.identifier type-argument-listopt
A namespace-name is a namespace-or-type-name that refers to a namespace.
Following resolution as described below, the namespace-or-type-name of a namespace-name shall refer to a namespace, or otherwise a compile-time error occurs. Type arguments (§25.5.1) shall not be present in a namespace-name (only types can have type arguments).
A type-name is a namespace-or-type-name that refers to a type. Following resolution as described below, the namespace-or-type-name of a type-name shall refer to a type, or otherwise a compile-time error occurs.
The syntax and semantics of qualified-alias-member are defined in §16.7.
A namespace-or-type-name that is not a qualified-alias-member has one of four forms:
-
I -
I<A1, ..., AK> -
N.I -
N.I<A1, ..., AK>
where I is a single identifier, N is a namespace-or-type-name and <A1, ..., AK> is an optional type-argument-
list. When no type-argument-list is specified, consider K to be zero.
The meaning of a namespace-or-type-name is determined as follows:
- If the namespace-or-type-name is a qualified-alias-member, the meaning is as specified in §16.7.
- Otherwise, if the namespace-or-type-name is of the form
Ior of the formI<A1, ..., AK>:
- If
Kis zero and the namespace-or-type-name appears within the body of a generic method declaration (§25.6) and if that declaration includes a type parameter (§25.1.1) with name I, then the namespace-or-type-name refers to that type parameter.
- If
- Otherwise, if the namespace-or-type-name appears within the body of a type declaration, then for each instance type
T(§25.1.2), starting with the instance type of that type declaration and continuing with the instance type of each enclosing class or struct declaration (if any):
- Otherwise, if the namespace-or-type-name appears within the body of a type declaration, then for each instance type
- If
Kis zero and the declaration ofTincludes a type parameter with nameI, then the namespaceor-type-name refers to that type parameter.
- If
- Otherwise, if
Tcontains a nested accessible type having nameIandKtype parameters, then the namespace-or-type-name refers to that type constructed with the given type arguments. If there is more than one such type, the type declared within the more derived type is selected. [Note: Non-type members (constants, fields, methods, properties, indexers, operators, instance constructors, finalizers, and static constructors) and type members with a different number of type parameters are ignored when determining the meaning of the namespace-or-type-name. end note]
- Otherwise, if
- Otherwise, for each namespace
N, starting with the namespace in which the namespace-or-typename occurs, continuing with each enclosing namespace (if any), and ending with the global namespace, the following steps are evaluated until an entity is located:
- Otherwise, for each namespace
- If
Kis zero andIis the name of a namespace inN, then:
- If
- If the location where the namespace-or-type-name occurs is enclosed by a namespace declaration for
Nand the namespace declaration contains an extern-alias-directive or using-alias-directive that associates the nameIwith a namespace or type, then the namespace-or-type-name is ambiguous and a compile-time error occurs.
- If the location where the namespace-or-type-name occurs is enclosed by a namespace declaration for
- Otherwise, the namespace-or-type-name refers to the namespace named
IinN.
- Otherwise, the namespace-or-type-name refers to the namespace named
- Otherwise, if
Ncontains an accessible type having nameIandKtype parameters, then:
- Otherwise, if
- If
Kis zero and the location where the namespace-or-type-name occurs is enclosed by a namespace declaration forNand the namespace declaration contains an extern-alias-directive or using-alias-directive that associates the nameIwith a namespace or type, then the namespace-or-type-name is ambiguous and a compile-time error occurs.
- If
- Otherwise, the namespace-or-type-name refers to the type constructed with the given type arguments.
- Otherwise, if the location where the namespace-or-type-name occurs is enclosed by a namespace declaration for
N:
- Otherwise, if the location where the namespace-or-type-name occurs is enclosed by a namespace declaration for
- If
Kis zero and the namespace declaration contains an extern-alias-directive or using-aliasdirective that associates the nameIwith an imported namespace or type, then the namespace-or-type-name refers to that namespace or type.
- If
- Otherwise, if the namespaces imported by the using-namespace-directives of the namespace declaration contain exactly one type having name
IandKtype parameters, then the namespace-or-type-name refers to that type constructed with the given type arguments.
- Otherwise, if the namespaces imported by the using-namespace-directives of the namespace declaration contain exactly one type having name
- Otherwise, if the namespaces imported by the using-namespace-directives of the namespace declaration contain more than one type having name
IandKtype parameters, then the namespace-or-type-name is ambiguous and an error occurs.
- Otherwise, if the namespaces imported by the using-namespace-directives of the namespace declaration contain more than one type having name
- Otherwise, the namespace-or-type-name is undefined and a compile-time error occurs.
- Otherwise, the namespace-or-type-name is of the form
N.Ior of the formN.I<A1, ..., AK>.Nis first resolved as a namespace-or-type-name. If the resolution ofNis not successful, a compile-time error occurs. Otherwise,N.IorN.I<A1, ..., AK>is resolved as follows:
- If
Kis zero andNrefers to a namespace andNcontains a nested namespace with nameI, then the namespace-or-type-name refers to that nested namespace.
- If
- Otherwise, if
Nrefers to a namespace andNcontains an accessible type having nameIandKtype parameters, then the namespace-or-type-name refers to that type constructed with the given type arguments.
- Otherwise, if
- Otherwise, if
Nrefers to a (possibly constructed) class or struct type andNcontains a nested accessible type having nameIandKtype parameters, then the namespace-or-type-name refers to that type constructed with the given type arguments. If there is more than one such type, the type declared within the more derived type is selected.
- Otherwise, if
- Otherwise,
N.Iis an invalid namespace-or-type-name, and a compile-time error occurs.
- Otherwise,
A namespace-or-type-name is permitted to reference a static class (§17.1.1.3) if
- The namespace-or-type-name is the
Tin a namespace-or-type-name of the formT.I, or
- The namespace-or-type-name is the
Tin a typeof-expression (§14.5.11) of the formtypeof(T)