Understanding Generics In C#

Introduction

 
This article is written to gain a clear understanding on Generics: why we use it, are there any workaround, why Generics are so important, and why we use this type-safe data structure rather than Standard Collections. This article is explained with basic examples of Generics for clear understanding. 
 

Generics

 
To the people who aren't familiar with Generics in C#, Generics is simply a class that gives us access to create classes and methods with a placeholder.
 
So, what is a placeholder?
 
In programming, the meaning of placeholder is a simple character or a word or something like a string of characters that takes the position of final data temporarily.
 
Sounds good! Now why do we need a character to be a placeholder?
 
If you are aware, whenever you go through a complex program you might have seen a Character T inside Angle brackets <> just like this <T> and we ask ourselves a question, What does this <T> mean and why do we need it?
 
To answer this question, <T> is a simple character inside angle brackets that's telling us: "Hey coders! I’m just a generic type parameter and please put me beside a class so that I will be useful as a Type in the place of a datatype, in your program."
 

If you’re still wondering what the above statement meant:

 
It’s saying that, at present, you can use me in place of a data type, and as per your need you can replace me with my datatype friends like int, string, etc., in your program.
 
This example code will suffice to gain a clear understanding of Generics:
  1. using System;    
  2. public class Hospital<T>     //Here <T> is placed beside class Hospital    
  3. {    
  4.   private T Cases;           //Here we declared a variable named "Cases" of Type T    
  5.   public Hospital(T value)   //Here in the constructor we took another variable named "value" of Type T    
  6.   {    
  7.    this.Cases=value;         //Here we're referring the fields of current class Hospital using this keyword.    
  8.   }    
  9.   public void Show()    
  10.   {    
  11.    Console.WriteLine(this.Cases);    
  12.   }    
  13. }    
  14. // Driver Class    
  15. class Test    
  16. {    
  17.   static void Main(string[] args)    
  18.   {    
  19.    Hospital<int> a = new Hospital<int>(100); // Here we can see <T> is replaced with <int> that means we are using <int> in place of <T>
  20.    Hospital <string> b = new Hospital<string("Hospital Cases"); // Here we can see <T> is replaced with <string> that means we are using <string> in place of <T>
  21.    a.Show();    
  22.    b.Show();    
  23.    Console.ReadLine();    
  24.   }    
  25. }    
We can make our Hospital class work on two Generic Types.
 
For example,
  1. using System;    
  2. class Hospital<T, U>   //Here we used two Generic types two work on Hospital Class    
  3. {    
  4.   public T PatientCount // Defining variable PatientCount of Type T    
  5.   {    
  6.    get;    
  7.    set;    
  8.   }    
  9.   public U Doctors      // Defining variable Doctors of Type U    
  10.   {    
  11.    get;    
  12.    set;    
  13.   }    
  14. }    
  15. // Driver Class    
  16. class Test    
  17. {    
  18.   static void Main(string[] args)    
  19.   {    
  20.     Hospital<intstring> a = new Hospital<intstring>();   //Here both Types T and U are replaced with datatypes int and string    
  21.     a.PatientCount = 100;    
  22.     a.Doctors = "Available";    
  23.     Console.WriteLine(a.PatientCount);    
  24.     Console.WriteLine(a.Doctors);    
  25.     Console.ReadLine();    
  26.   }    
  27. }   
As a student or as a Software Developer, you might have heard over and over again that Generics are type-safe and also C# is a type-safe language.
 
You might have also learned that Object Class is the Base Class for all .Net Classes. So, what I’m trying to focus on here is, for an instance if you think,
 

Why do we need generics? Don’t we have Standard Collections?

 
To answer, let us dig more into Generics and Collections. The Primary idea of using Generics is, our “Standard Collections” can’t do an effective type-checking. If it cannot do an effective type checking, obviously it is not our friend, because as I pointed out earlier we are dealing with type-safe because our C# is a type-safe language and this guy here “Standard Collection” is not good at type-safe. So we ended up with Generics, a beautiful and powerful feature introduced in C# 2.0. Choosing Generics we're aiming at significant performance boost and quality code and without duplicating our code we're reusing our data. So, we are achieving reusability here.
 
Generics are also helpful in other areas of .Net such as arrays, reflection, serialization, collections, remoting, etc.
 
You can dig more into  Generics here.
 
Hope this helps, and Happy Coding!