泛型使用开放和封闭泛型类型的概念:参数化泛型类定义(即List< T>)是一种开放泛型类型,运行时为代码中的每个不同用途生成一个封闭的泛型类型,即不同的类型为List< int>创建了类型和列表< MyStruct> – 对于每个封闭的泛型类型,T的大小和类型在运行时是已知的.
When a generic type or method is
compiled into Microsoft intermediate
language (MSIL),it contains metadata
that identifies it as having type
parameters. How the MSIL for a generic
type is used differs based on whether
the supplied type parameter is a value
type or reference type.
When a generic type is first
constructed with a value type as a
parameter,the runtime creates a
specialized generic type with the
supplied parameter or parameters
substituted in the appropriate
locations in the MSIL. Specialized
generic types are created one time for
each unique value type that is used as
a parameter.
Generics work somewhat differently for reference types. The first time a generic type is constructed with any reference type,the runtime creates a specialized generic type with object references substituted for the parameters in the MSIL. Then,every time that a constructed type is instantiated with a reference type as its parameter,regardless of what type it is,the runtime reuses the previously created specialized version of the generic type. This is possible because all references are the same size.