Basics Of Generics In C#

Generics was introduced in C# 2.0 to allow the delay of DataType specification in a class or a method. Due to Generics, we are able to write a method or a class that can work with any DataType and whose DataType can be defined later, as per the need. A Generic class can be defined, using angle brackets <>.
 
Generic class can be defined by putting the <T> sign after the class name.
  1. public class MyClass<T>   
  2. { }  

The example is given below of simple Generic class.

  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             //Instantiating the Generic Class  
  6.             //See here we have given the dataType as Int  
  7.             //SampleGenericClass<int> intGenericClass = new SampleGenericClass<int>(10);  
  8.   
  9.             SampleGenericClass<string> stringGenericClass = new SampleGenericClass<string>("TEST");  
  10.   
  11.             string value = stringGenericClass.samplegenericMethod("TESTING");  
  12.             Console.ReadLine();  
  13.         }  
  14.   
  15.   
  16.   
  17.     }  
  18.   
  19.     class SampleGenericClass<T>  
  20.     {  
  21.         private T genericMemberVariable;  
  22.   
  23.         //Constructor Where T can be of any type  
  24.         public SampleGenericClass(T value)  
  25.         {  
  26.             genericMemberVariable = value;  
  27.         }  
  28.   
  29.         public T samplegenericMethod(T genericParameter)  
  30.         {  
  31.             Console.WriteLine("Parameter type: {0}, value: {1}"typeof(T).ToString(), genericParameter);  
  32.             Console.WriteLine("Return type: {0}, value: {1}"typeof(T).ToString(), genericMemberVariable);  
  33.   
  34.             return genericMemberVariable;  
  35.         }  
  36.   
  37.         public T samplegenericProperty { getset; }  
  38.     }   
Output
 
 
Here, the code is given below.
  1. SampleGenericClass<string> stringGenericClass = new SampleGenericClass<string>("TEST");  
We are passing string as a DataType for a class. T. T has been replaced by the string DataType.
 
The class given above will be seen, as shown below.
  1. class SampleGenericClass<T>  
  2.   {  
  3.       private string genericMemberVariable;  
  4.   
  5.       //Constructor Where T can be of any type  
  6.       public SampleGenericClass(string value)  
  7.       {  
  8.           genericMemberVariable = value;  
  9.       }  
  10.   
  11.       public string samplegenericMethod(string genericParameter)  
  12.       {  
  13.           Console.WriteLine("Parameter type: {0}, value: {1}"typeof(string).ToString(), genericParameter);  
  14.           Console.WriteLine("Return type: {0}, value: {1}"typeof(T).ToString(), genericMemberVariable);  
  15.   
  16.           return genericMemberVariable;  
  17.       }  
  18.   
  19.       public string samplegenericProperty { getset; }  
  20.   }  
It has been replaced with the DataType string. It can be replaced by the DataType passed while initializing the class.
 
The example given below uses an integer.
  1. SampleGenericClass<int> intGenericClass = new SampleGenericClass<int>(1);  
  2.   
  3.             intGenericClass.samplegenericProperty = 2;  
  4.             int result = intGenericClass.samplegenericMethod(3);  
Output for this.


We can apply Generics to,
 
  • Interface
  • Abstract class
  • Class
  • Method
  • Static method
  • Property
  • Event
  • Delegates
  • Operator 

Advantages

  1. It Increases the reusability of the code.
  2. These are safe types.
  3. Performance is better because of the removal of boxing and unboxing possibilities.
  4. Common use of Generic class is to create collection classes.
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now