1. The reference type constraint: type arguments comprise any class, interface, array, commission or other known type is a reference type of the parameter
class demo<T> where T:class
An effective seal section demo <String>; demo <int []>; demo <person>; demo <IDisposable>
Invalid block section demo <int>
2. The value type constraint: contains a value type, comprising enumeration enum. But he will be excluded empty type int?
class demo<T>where T:struct
An effective seal section demo <int>; demo <enumDemo>
Invalid block section demo <int []>; demo <object>; demo <StringBuild>
3. Type constructor constraint: None constructor constraint parameters
Generic method demo
Public T demo<T>() where T:new()
{
return T()
}
4 conversion type constraints: type arguments can be consistent, reference or boxing conversion into the implicit type
class demo<T> where T:int
demo <int>; Consistency conversion
class demo<T> where T:IDisposable
demo <SqlConnection> reference conversion
public class demo<T> where T:IComparable<T>
demo <int> demo <float> boxing conversion
class demo<T,U> where T:U
demo <int, IComparable> boxing conversion
You can specify multiple interfaces but can only specify a class
class demo<T> where T:IComparable<int>,Stream,IDisposable
5. The composition constraints: both without any type is a value type and reference type, so that this combination is prohibited
It has been constrained to a value type that can no longer be bound by new ()
If one is to convert the type of constraint is the kind of thing that will be on the front of the interface, and we can not specify the same interface more than once.
Effective constraint: class demo <T> where T: class, new ()
class demo<T> where T:Struct,IDisposable
class demo<T,U> where T :class where U :struct