C # generics. The C # generics are explained in detail and examples on msdn. In the learning process, you should study the above msdn examples, because they are the most essential, even if they are not the easiest to understand.
The following is an excerpt from the Section C # Generic and C ++ templates. For review
------------------------------------------------------------------------------
C # generics and C ++ templates are used to provide language functions supported by parameterized types. However, there are many differences between the two. At the syntax level, C # generics are simpler methods for implementing parameterized types, without the complexity of C ++ templates. In addition, C # does not try to provide all the functions provided by the C ++ template. At the implementation level, the main difference is that C # Generic Type replacement is executed at runtime, so that the generic type information is reserved for the instantiated object. For more information, see
Generics in Runtime (C # programming guide).
The main differences between C # Generic and C ++ templates are as follows:
-
C # Generics do not provide the same degree of flexibility as C ++ templates. For example, although User-Defined operators can be called in a C # generic class, Arithmetic Operators cannot be called.
-
C # non-type template parameters are not allowed, for example, template C {} .
-
C # does not support explicit customization, that is, the custom implementation of a specific type of template.
-
C # partially exclusive: custom implementation of type parameter subsets.
-
C # The type parameter cannot be used as the base class of the generic type.
-
C # The type parameter is not allowed to have the default type.
-
in C #, although the construction type can be used as a generic type, the generic type parameter itself cannot be a generic type. C ++ does allow template parameters.
-
C ++ allows Code that may not be valid for all type parameters in the template, then, check whether the code is useful for the specific type of the type parameter. C # write the code in the class accordingly so that any type that meets the constraints can be used. For example, you can write Arithmetic Operators for objects of type parameters in C ++
+ and - function, this will produce an error when using the types that do not support these operators to instantiate the template. C # This is not allowed. The only allowed language structures are those that can be derived from constraints.