This article is a Microsoft's technical article for translation. For reference for learning C #, please do not use for commercial purposes. http://msdn.microsoft.com/vcsharp/team/language/default.aspx
Because this chapter is very long, you may need a few :) 20. Pan-type 20.1 Pan-type declaration generic category is a statement that needs to provide type parameters to form an actual type.
Class declarations can be selected to define type parameters. class-declaration: (class declaration) attributesopt class-modifiersopt class identifieropt type-parameter-listopt class -baseopt type-parameter-constraints-clauseopt class-body; opt (optional characteristic class identifier class modifier optional optional type parameter List Optional Base Class Option Class Parameter Constraints Optional Categories; Optional)
Unless the type of parameter list is provided, the class declaration may not provide the type parameterized constraint statement. A class declaration that provides a list of type parameters is a generic class declaration. In addition, any class embedded in the generic class declaration or generic structure declaration, it is a generic category, as the type of type parameters containing types must be provided to create a constructed type; generic class by using construction types Reference (20.5). Give Pancase Declaration Class List >. The structure type can be used for one or more parameters, such as List
The generic type cannot be "overloaded"; otherwise, in one scope in a scope, with a normal type, the generic type must be uniquely named.
Class C {} Class C
20.1.1 Type Parameter Type Parameters can be provided on a class declaration. Each type parameter is a simple identifier that indicates a placeholder for type parameters for creating a constructor. Type parameters are the form of placeholders in the type of type that will be provided later. Instead, type parameters 20.5.1) just an alternative to the actual type when the constructor is referenced.
Type-parameter-list :( Type Parameter list :)
Type parameters cannot be used directly to declare a base type or interface
For members lookup rules on the type parameters, if the constraint exists, depending on the constraint applied to the type of parameters. See §20.7.4 in more detail.
Feasible conversion of type parameters depends on the constraint (if any) applied to the type of parameters. See §20.7.4 in detail.
Field NULL cannot be converted to the type given by the type parameters, unless the type parameter is constrained by a class constraint (20.7.4). However, a default expression (§20.8.1) can be used instead. In addition, the values of the type given by one type parameter can be compared using "==" and "! =" (§20.8.4) with NULL.
If the type parameter is constrained by a constructor-constraint (20.7), the New expression can only be used with a type parameter.
Type parameters cannot be used anywhere in the characteristics.
Type parameters cannot be used for members access, or represent a static member or a type name of the nested type (§20.9.1, 20.9.4).
In an insecure code, type parameters cannot be used as managed types (§18.2).
As a type, type parameters are purely just a compile time. At runtime, each type parameter is bound to the runtime type, which is specified by the type of the type provided by the generic type declaration. To do this, the variable type of the type parameter declared is a closed type (20.5.2) when running. CLOSED TYPE. All statements and expressions perform the type parameters used at runtime, are the actual types provided by that parameter as the type of party.
20.1.2 Example Types Each class declaration has a constructed type associated with it, ie instance type (Instance Type). For a generic class declaration, the instance type is formed by creating a constructor (20.4) from the type declaration (§20.4), which uses each type corresponding to the type parameters. Since the instantiation type uses the type parameters, it is only valid within the type parameter scope (within the class declaration). The instance type is this type of this declaration. For non-extensive classes, the instance type is just a statement. Several declaration classes are shown below, as well as their instance types. Class A
Class Extend
20.1.4 All members of the general class of generic classes can use the type parameters from any closure class (Enclosing Class) directly or as part of the enclosing class. When a particular closed constructor is used, each use of the type parameter is replaced by the actual type of the constructive provided by the constructive type. E.g
Class C
Public c (v x) {this.f1 = x; this.f2 = this;}} class application {static void
Main
() {C
() {C
[1] This is easy to understand, because at runtime, different enclosed constructive types belong to different types, such as List