Using C # <T> generic

When defining generic class, the client may be able to impose restrictions on the type of the code kind type parameter when the class is instantiated. If the client code tries to use a type of constraint does not allow the class to instantiate, compile-time error results. These limits are called constraints. Use the keyword context where constraints are specified.
Listed below are the six types of constraints:

Constraints Description

T: Structure  
Type parameter must be a value type. You can specify any value other than the type of Nullable. For more information, see Using nullable types (C # Programming Guide).

T: class  
type parameter must be a reference type, including any class, interface, or delegate array type.

T: new ()  
type parameter must have a public constructor with no arguments . When used in conjunction with other constraints, new () constraint must be specified last.

T: <base class name>  
type parameter must be specified in the base class is derived from the base class, or specified.

T: <Interface name>  
Type parameter must be specified interface or implement the specified interface. Constraints can specify multiple interfaces. Constraints interface may also be generic.

T: U  
type parameter T must be provided for the U parameter supplied to or derived from parameters U provides. This is called a naked type constraint.

Why use constraints:
If you want to check an item in a generic list to determine whether it is valid or compare it with some other items, the compiler must guarantee a certain extent, the operator needs to call or method it will be subject to client code It may support any type parameters specified. This is to ensure that the generic class defined by applying one or more constraints obtained. For example, the base class constraints tell the compiler: this type of object or objects derived from this type only was used as a type parameter. Once the compiler has the guarantee, it is possible to allow this type of method calls the generic class. Constraints are using a context where key applications.

There are five substantially generic constraints:
the value type constraint: must be generic parameters required value type, e.g. int, short, and other custom stuct
    public class MyClass2 <T>
        WHERE T: struct // only accepts this generic class generic type parameter values
    {
    }
reference type constraint: requirements of parameters must be generic type, e.g. string, object, and a custom class
    public class MyClass <T>
        WHERE T: the generic class class // only accept reference type of generic parameters
    {
    }
constructor constraint: must have generic parameters required constructor
    public class MyClass3 <T>
        WHERE T: new new ()
    {
    }
interface constraints: generic parameter requirements must implement an interface
    MyClass4 class public <T>
        WHERE T: the System.IComparable
    {
    }
base class constraints: Generic Requirements parameter must inherit a base class
    public class MyClass5 <T>
        WHERE T: the Customer
    {
    }

Guess you like

Origin www.cnblogs.com/zhang1f/p/11667012.html