Generics are the most powerful feature of C# 2.0. It allows defining type-safe data structures, without committing to actual data types. But in most of the application we come across situation where we need type that can hold both reference & value type. In such cases we need only either value type or reference type.
For example:List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add("hello"); // compiler error!
As we know List is inbuild generic collection that can have value type as well ref type based on the it will validate its data.
In above example compilation error throwing reason that List initialized with int type ie. value type.
Now question how to create either value type or reference type custom generic that enforce validation at intialize time itself.
Solutions: RefType: class Employee <T> where T: class
{ }
ValueType class OnlyNumber <T> where T: struct { }
From a above Employee class is ref type generic that can have only reference data type .
and OnlyNumber class where T points to struct can have only number that means only value type.