Generic properties

Generic
concept generics: generic multi-operation is accomplished in the same type of a parameter of the
data types of technology

Using a "parameterized type," the abstract type, thereby realizing flexible multiplexing

Advantage of generics:
1. Generic enhance the readability of the code
2. generic help to achieve code reuse, type of protection and safety to improve performance
3. We can create a generic collection classes

4. The parameterized generic types and methods

5. We can also be constrained in a generic class method to access a particular data type
6. Information regarding the type of generic data type may be used at run time by using the
acquired reflection

Using generic type default value Default value is 0, the default value is a null reference type


For this type with a type parameter, we call: open-ended type; while
on the type of the actual parameter is passed in the parameter type is called: Closed Type

Generic method provides a more varied flexibility. Generic methods may be present in the generic class
may be that the non-generic classes. You can type parameters as parameters of a method,
the return value, or a local variable, the type parameter may not be needed for the entire class,
and more specifically to a method for.

The principle of generics
implement generic type is erased by technology type erasure is done at compile time, and
that is, at compile time, the compiler will generic type parameters are erased to its limits
given type , then if there is no type object is erased when the acquired again strong
braking type into a corresponding type. During the run and no generics,
so there is no optimization.

Generic constraints: format where T: Constraint needs to match
struct type constraint is a value
class is a reference type constraint
new () constraint has a null constructor, if it contains new () constraint must be placed after the last
base class name constraint requirements may be passed type base class (parent class) itself, it may be a derived class (subclass)
interface name constraints require that type can be passed in the base class interface (parent class) itself, may be it is a derived class (subclass)

 

 

Guess you like

Origin www.cnblogs.com/lxlxlx/p/11166301.html