Basics Of Generic Classes In C#

This article explains how to use a Generic class and why we need to use it.

Introduction 

 
I would like to share the basics of generics. Here first we will learn the problem statement and then resolve the problem using Generics.
 

Problem statement

 
First create a class as in the following code.
  1. class CompareClass {  
  2.     public bool Compare(string x, string y) {  
  3.         if (x.Equals(y)) return true;  
  4.         else return false;  
  5.     }  
  6.     public bool Compare(int x, int y) {  
  7.         if (x.Equals(y)) return true;  
  8.         else return false;  
  9.     }  
  10. }  

Understanding the code

 
We created the CompareClass.
 
Here we created two compare methods, one for the string data type and the second for an int data type.
 
So the class contains overloaded compare functions.
 

Problem

 
So if we need to compare other datatypes like decimal, double and objects, then the code above would not work and we need to create another method to compare the proposed data type. We can solve this problem with generics.
 
Basics Of Generic Classes In C#
 

Solution


Create a class as in the following code,
  1. class CompareGenericClass < T > {  
  2.     public bool Compare(T x, T y) {  
  3.         if (x.Equals(y)) return true;  
  4.         else return false;  
  5.     }  
  6. }  

Understanding the code

  1. We created the class CompareGenericClass with the input parameter T so the class is CompareGenericClass<T>
  2. Here T would be the datatype.
  3. If we want to compare strings then the following style would be used to create an object of the class,
    1. CompareGenericClass<string> Ocompare = new CompareGenericClass<string>();    
    2. bool stringResult=Ocompare.Compare("DEVESH""DEVESH");    
    Basics Of Generic Classes In C#

  4. Since we passed T as a string the Compare method will accept only a string type of parameter.
  5. We did the same for Interger as well. 
    1. CompareGenericClass<int> oIntcompare = new CompareGenericClass<int>();       
    2. bool integerresult=oIntcompare.Compare(5, 6);   
    Basics Of Generic Classes In C#

  6. Here with this class we do not need to overload the Compare method because this compare method accepts only a parameter that has been passed during creation of class objects.
  7. Using this class we can avoid such problems that have been discussed above.

Output of code

 
Basics Of Generic Classes In C#
 

Complete code 

  1. class CompareClass {  
  2.     public bool Compare(string x, string y) {  
  3.         if (x.Equals(y)) return true;  
  4.         else return false;  
  5.     }  
  6.     public bool Compare(int x, int y) {  
  7.         if (x.Equals(y)) return true;  
  8.         else return false;  
  9.     }  
  10. }  
  11. class CompareGenericClass < T > {  
  12.     public bool Compare(T x, T y) {  
  13.         if (x.Equals(y)) return true;  
  14.         else return false;  
  15.     }  
  16. }  
  17. class Program {  
  18.     static void Main(string[] args) {  
  19.         CompareClass obj = new CompareClass();  
  20.         bool intresult = obj.Compare(5, 7);  
  21.         Console.WriteLine("int comapre result:" + intresult);  
  22.         bool stringresult = obj.Compare("DEVESH""DEVESH");  
  23.         Console.WriteLine("string comapre result:" + stringresult);  
  24.         CompareGenericClass < string > Ocompare = new CompareGenericClass < string > ();  
  25.         bool stringResult = Ocompare.Compare("DEVESH""DEVESH");  
  26.         Console.WriteLine("Generic string comapre result:" + stringResult);  
  27.         CompareGenericClass < int > oIntcompare = new CompareGenericClass < int > ();  
  28.         bool integerresult = oIntcompare.Compare(5, 6);  
  29.         Console.WriteLine("Generic int comapre result:" + integerresult);  
  30.     }  
  31. }  

Conclusion

 
We have learned how to use a Generic class and why we need to use it.