Generic Type Instantiations
Fan model
Similar to a non-generic type, the compiled representation of a generic type is intermediate language (IL) instructions and metadata. The representation of the generic type of course also encodes the existence and use of type parameters.
Similar to non-floats, generics are compiled into intermediate code instructions and metadata. The generic representation is of course also encoding the existing and use type parameters.
The first time an application creates an instance of a constructed generic type, such as Stack
When the application creates a newly constructed generic, such as stack
The .NET Common Language Runtime creates a specialized copy of the native code for each generic type instantiation with a value type, but shares a single copy of the native code for all reference types (since, at the native code level, references are just pointers WITH THE SAME REPRESENTATION.
To create a special local code copy for generics for each value type. But share a separate local code copy for all reference types. (Because, in the local code level, reference and pointer are the same indication)
19.1.2 Constraints
constraint
Commonly, a generic class will do more than just store data based on a type parameter. Often, the generic class will want to invoke methods on objects whose type is given by a type parameter. For example, an Add method in a Dictionary
In general, a generic class can not only store data on the type parameters, but also do more. Typically, generic classes attempt to call methods on the specified type object. For example, an Add method in Dictionary
However, type parameter k may be any type, and the only member variable that is assumed to be in the Key parameter is declared by the object type, such as Equal, GetHashCode, and Tostring; the above code will trigger a compile time error. Of course, you can also put the key.
The parameter is converted into a type containing the Compareto method. For example, the Key parameter may be converted into an IComparable interface.
Public Class Dictionary
When the above solution runs, add a dynamic type check at the above code requirements. And it reports an error when running and throws an InvalidCastException when Key does not support IComparable interface.
To provide stronger compile-time type checking and reduce type casts, C # permits an optional list of constraints to be supplied for each type parameter. A type parameter constraint specifies a requirement that a type must fulfill in order to be used as an argument for that type parameter. Constraints are declared using the word where, followed by the name of a type parameter, followed by a list of class or interface types, or the constructor constraint new (). in order to provide stronger compile-time type checking and reduction type Conversion, C # allows an optional constraint list to provide each type of parameters. As a type of parameter constraint, a type parameter constraint specifies a type that must be fully fulfilled. Constrained via the keyword where declaration, followed by the name of the type parameters, follow up a string or interface, or a constraint constructor new ()
In Order for the Dictionary
In order to ensure the KEY of Dictionary
Public Class Dictionary
After specifying, the compiler will ensure that any type of parameters supplied to K must support the IComparable interface. Moreover, before calling the CompareTo method, it no longer needs to explicitly support the key parameter to support the IComparable interface. All members given by a type of parameters are visible directly on the type value.
For a given type parameter, it is possible to specify any number of interfaces as constraints, but no more than one class. Each constrained type parameter has a separate where clause. In the example below, the type parameter K has two interface constraints, while The Type Parameter E HAS A Class Constraint and a Constructor Constraint: As a given type parameter, but a class may specify any number of interfaces as constraints. Each constraint type parameter has a WHERE clause separation. In the following example, the type parameter k has two interface constraints, however the type parameter E has a class constraint and a build constraint.
Public Class EntityTable
Construction constraints in the above example () ensure that the type of type E has a public, non-parameter build function, and it allows generic classes to create instances of this type using new e ().
Type parameter constrains should be used with care. While they provide stronger compile-time type checking and in some cases improve performance, they also restrict the possible uses of a generic type. For example, a generic class List
Type parameter constraints should be careful. They offer stronger compile time type checks and can improve performance in some examples, and can constrain generic usage possibilities. For example, a List