Generic Types in C# 2.0 with Sample

Generic Types

 
Generics are the most powerful feature of C# 2.0. It allows defining type-safe data structures, without committing to actual data types. In C# 1.0 we can either declare reference type or value type. But in most of the application we come across situation where we need type that can hold both reference & value type. In such situation we use generic types.
 

Why Generics? 

  1. Generic type doesn't care what the type is. We can specify the type at runtime.
  2. It avoids boxing difficulties. In C# 1.0, if we want to put any object into a List, Stack, or Queue objects, we have to take type as System.Object.
  3. It boosts the performance of the application because we can reuse data processing algorithm without duplicating type-specific code.

How Generic implemented

  1. Generic type is instantiated at run-time not compiled time
  2. Generic type are checked at time of declaration not at instantiation
  3. It works for both reference type & value type.

Let's create simple class "GenericList" using C# 1.0 & 2.0 respectively & compare them.
 
Code GenericList Class (C# 1.0)
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;   
  4. public class GenericList  
  5. {  
  6.         private  object[] elements;  
  7.         private int count;   
  8.         public GenericList()  
  9.         {  
  10.             elements = new object[10];  
  11.         }  
  12.         public object this[int index]  
  13.         {  
  14.             get { return elements[index]; }  
  15.             set { elements[index] = value; }  
  16.         }   
  17.         public void Add (object parm)  
  18.         {  
  19.             if (count == elements.Length)  
  20.             {  
  21.                   // Increase the  
  22.                   object[] tmpArray = null ;  
  23.                   elements.CopyTo(tmpArray,0);  
  24.                   elements = new object[count * 2];  
  25.                   elements = tmpArray;                               
  26.             }  
  27.             elements[count] = parm;  
  28.             count = count + 1;  
  29.         }  
  30. }  
Main Method
  1. static void Main(string[] args)  
  2. {  
  3.     Console.WriteLine("using C# 1.0");   
  4.     GenericList list = new GenericList();  
  5.     list.Add(20);        //Argument is boxed  
  6.     list.Add(40);        //Argument is boxed  
  7.     list.Add("Sixty");   //Error in retrieving  
  8.     Console.WriteLine("Item Added");   
  9.     int val = (int)list[0]; //Casting required  
  10.     Console.WriteLine("Value retrived : " + val);   
  11. }  

Memory Consumption 

 
In C# 1.0 boxing is necessary evil to make type system work. While working with structures of System.Collection namespace (Stacks,List,Hashtable etc) we face the problem in insertion & retrieval of values. We need to take System.object as type & System.object is reference type, so whenever we access the hashtable, the runtime has to box the values to put into the collection & need to unbox to take it out.
  1. list.Add(20);        //Argument is boxed  
In C# int takes 4 byte but when it boxed it take (4+8) 12 bytes, which is 3 times to normal size.
 
In C# 2.0 the type is decided at runtime so boxing does not take place.
 

Type Safe

 
When we use the statement
 
list.Add ("Sixty"); or List [3] = "sixty";
 
It compiles successfully but later on if some one pulls value and cast it into integer it fails. The problem is fixed in C# 2.0; we will get compilation error there.

Code GenericList Class (C# 2.0)

  1. public class GenericList<T>  
  2. {  
  3.     public GenericList()  
  4.     {  
  5.         elements = new T[10];  
  6.     }  
  7.     private T[] elements;  
  8.     private int count;  
  9.     public T this[int index]  
  10.     {  
  11.         get { return elements[index]; }  
  12.         set { elements[index] = value; }  
  13.     }  
  14.     public void Add(T parm)  
  15.     {  
  16.         if (count == elements.Length)  
  17.         {  
  18.             T[] tmpArray = null;  
  19.             elements.CopyTo(tmpArray, 0);  
  20.             elements = new T[count * 2];  
  21.             elements = tmpArray;  
  22.         }  
  23.         elements[count] = parm;  
  24.         count = count + 1;  
  25.     }  
  26. }  
Main Method
  1. static void Main(string[] args)  
  2. {  
  3.     Console.WriteLine("using C# 1.0");  
  4.     GenericList<int> genericList = new GenericList<int>();  
  5.     genericList.Add(10);          //No boxing  
  6.     genericList.Add(20);          //No boxing  
  7.   
  8.     // genericList.Add("Fifty");   //Compile Time Error  
  9.     Console.WriteLine("Item Added");  
  10.     int valGeneric = (int)genericList[0]; //No Casting Required  
  11.     Console.WriteLine("Value retrived : " + valGeneric);  
  12. }  
Some other Points

(1) Type parameter can be applied to Class, struct, interface & delegates.

 struct Buket<K, V>;
 interface ICompare<T>

(2) Type parameter can have constraints. 

Constraint Description
Public class Books where T: struct The type argument for class Books must be a value type
Public class Books where T: class The type argument for class Books must be a reference type
Public class Books where T: new( ) The type argument for class Books must have a public default constructor
Public class Programmer where T: <Employee> The type argument for class Programmer must be of Employee type.


Similar Articles