In this blog I not tell you “What is generic class in C#?”
you can read about generic from below link.
Here I tell you “Why Generic in C# !”. First see below code:
Whenever we define the class in C#, C++ or in
Java, class can have several types of variables.
How in this case if we create an object, object
consist several variables of different types.
Now we want to create multiple object of same
class but every object will have different type of variable called collection.
Here come boxing and unboxing.
Conclusion: need to take care about conversion before
assigning the data.
Now the main purpose of the using generics, each
object will have multiple variable of similar type. Now we can prevent boxing
If we apply generic on class then we can achieve static
mindstick <int> ms1 = new mindstick<int>();
mindstick <string> ms2 = new mindstick<string>();
Now in above program, we have two objects belong to same
class supporting different data type not in same.
This type of an object is called “Generic Object”
and its class known as “Generic Class”. See below example.
public class caller
<int> k1= new <int>();
<string> k2= new <string>();
int t1 =
int k1.assign(12); // No change to apply conversion