ECMA-334: 25.1 Generic class declarations
25.1 Generic class declarations
A generic class declaration is a declaration of a class that requires type arguments to be supplied in order to form runtime types.
[Note: A class declaration (§17.1), can optionally define type parameters and their associated constraints:
- class-declaration:
- attributesopt class-modifiersopt
partialoptclassidentifier type-parameter-listopt class-baseopt type-parameter-constraints-clausesopt class-body;opt
- attributesopt class-modifiersopt
A class declaration shall not supply type-parameter-constraints-clauses (§25.7) unless it also supplies a type-parameter-list (§25.1.1).
A class declaration that supplies a type-parameter-list is a generic class declaration. end note]
Any class nested inside a generic class declaration or a generic struct declaration (§25.2) is itself a generic class declaration, since type parameters for the containing type shall be supplied to create a constructed type.
Generic class declarations follow the same rules as non-generic class declarations except where noted. Generic class declarations can be nested inside non-generic class declarations.
A generic class is referenced using a constructed type (§25.5). [Example: Given the generic class declaration
class List<T> {}
some examples of constructed types are List<T>, List<int> and List<List<string>>. end example] A constructed type that uses one or more type parameters, such as List<T>, is called an open constructed type (§25.5). A constructed type that uses no type parameters, such as List<int>, is called a closed constructed type (§25.5).
Generic types can be "overloaded" on the number of type parameters; that is two type declarations within the same namespace or outer type declaration can use the same identifier as long as they have a different number of type parameters.
class C {} class C<V> {} // OK struct C<U,V> {} // OK class C<A,B> {} // Error, C with two type parameters defined twice
The type lookup rules used during type name resolution (§10.8), simple name resolution (§14.5.2) and member access (§14.5.4) respect the number of type parameters.
The base interfaces of a generic class declaration shall satisfy the uniqueness rule described in §25.3.1.