C # where generic type constraint

Generic definition  where clause specifies the parameter type of constraint as a generic type of method, the local function, or delegate type parameter. Constraint may specify interface requirements of a generic type base or a reference value or unmanaged type. They declare type parameters must have features.

As the type of the interface used must be bound, non-sealing type or a type parameter. 

You can specify a generic class, struct, System. The Enum, System. Delegate, System. MulticastDelegate etc.

 

Example to illustrate this, we now define a FunTest <T> function, we add constraints by where T 

T is a specified type, and the interface is in the class ITransformer (after the comma can not write)

public static void FunTest<T>() where T : class, ITransformer
        {

        }

 

Guess you like

Origin www.cnblogs.com/z45281625/p/11357494.html