The last two constraints which explained in the previous posts enable you to indicate that a type argument must be either a reference type or a value type. These constraints are useful in the few cases where the difference between reference and value types is important to generic code. The general forms of the where statements for these constraints are shown here:
where T : class
where T : struct
Here, T is the name of the type parameter. When additional constraints are present, class or struct must be the first constraint in the list.
Here is an example that demonstrates the reference type constraint:
First, notice how Test is declared:
The class constraint requires that any type argument for T be a reference type. In this program, this is necessary because of what occurs inside the Test constructor:
Here, obj (which is of type T) is assigned the value null. This assignment is valid only for reference types. In C#, you cannot assign null to a value type. Therefore, without the constraint, the assignment would not have been valid, and the compile would have failed. This is one case in which the difference between value types and reference types might be important to a generic routine.
The value type constraint is the complement of the reference type constraint. It simply ensures that any type argument is a value type, including a struct or an enum. Here is an example:
In this program, Test is declared as shown here:
Because T of Test now has the struct constraint, T can be passed only value type arguments. This means that Test<MyStruct> and Test<int> are valid, but Test<MyClass> is not. To prove this, try removing the comment symbol from the start of the last line in the program and recompiling. An error will be reported.
Technorati : Constraints in C#