articles

Home / DeveloperSection / Articles / Generics in C# with example

Generics in C# with example

Amit Kumar2025 14-Jun-2017

Array : Array is strong type because there is no boxing and unboxing. It's performance is better than other collection.But there is a weak point is that it is a fixed length.

 
 And ArrayList and HashTable is resizable but weak point is that it takes only objects and then It has more boxing and unboxing.
 
After reading above both line software developers think that, we have read any thing there is some lackess is very topic or every where.
I think one question is arising in software developers mind that if C# provide any other collection which fulfill both above lackness. 
Now software developers want to take advantage of both then one picture comes in mind That is Generic Collection. Generic collection help us to create strong, flexible type collection easy. 
 
First we are talking about Generics then Generic Collection.
 
Generics are the most power features in C# 2.0 .
Generics allow you to define type-safe classes without compromising type safty, performance or productivity. Generic introduce to the .NET Framework the concept of type parameter, which make it possible to design classes and methods. 
 
Example: I want to compare two values  first I will do it with simple class after that we will do it by using Generic .
 
Let’s try to understand by a practical example. 
 
Open Visual Studio.
 
Generics in C# with example 
 
 Click on Project and set the name Application is MyFirstGenericApplication
 
Generics in C# with example 
 Now try to use first method means simple class
 
Add a Check Class  
  1. public class Check  
  2.    {  
  3.        public bool Compare(int a, int b)  
  4.        {  
  5.            if (a == b)  
  6.            {  
  7.                return true;  
  8.            }  
  9.            else  
  10.            {  
  11.                return false;  
  12.            }  
  13.        }  
  14.    }  
 After creation the class we create an object of Check class and use compare function like.
 
  1. class Program  
  2.    {  
  3.        static void Main(string[] args)  
  4.        {  
  5.            Check objCheck = new Check();  
  6.            bool result = objCheck.Compare(1, 4);  
  7.            Console.Write(result);  
  8.            Console.ReadKey();             
  9.        }  
  10.    }  
Output: 
 
Generics in C# with example 
 
As you see above for compare two integer numbers I have to create Compare function, think about if I want to compare two string or two float values then I have to create seperate method for each. So solve this issue Generic comes.
 
Generic allow to define the classes or function of any type. 
 
If I define  a class of any type and compare function parameter's type is any type(REPLACEDTATYPE), then If I have pass integer datatype in class type at runtime then parameter type is also integer type or pass string type then parameter type is also string. It will happen using Generic Collection then we must have to add reference of Using System.Generic.Collections in our project.
 
I have create same class with Class with type and function parameter  with also type. 
  1. public class Check <REPLACEDATATYPE>  
  2.     {  
  3.         public bool Compare(REPLACEDATATYPE a, REPLACEDATATYPE b)  
  4.         {  
  5.             if (a.Equals(b))  
  6.             {  
  7.                 return true;  
  8.             }  
  9.             else  
  10.             {  
  11.                 return false;  
  12.             }  
  13.         }  
  14.     }  
Now you can see Check class contains  REPLACEDATATTYPE.
 
Now create and object of Check class  then is showing compile time error which you can see below..
 

Updated 23-Dec-2017

Leave Comment

Comments

Liked By