Understanding <T> In C#

Many times when we talk about generics, we only understand List <>. But generics are totally depending on <T>. The feature of generics incorporated in Framework 2.0 enhances significant performance improvement and reusability of objects.

Features of Generic

  1. Type Safe: Generics are type safe (Strongly Type). You can store any type of data in that.

    In the above example I have defined list of string type and as you can see I can only inserted string value into it.

    list

  2. Performance: No doubt the performance is faster. To get into the performance you need to understand the concept of Boxing and Unboxing. When a value type is converted into object is called Boxing. And the vice versa is called unboxing. When you add something into non generic collection you need add everything as an Object.

    Try this:

    Array list

    Same exists for the other non-generic type collection i.e. HashTable, Stacks etc.

  3. Reusability: Well yes generics are reusable objects. “Define and use” concepts are applicable on generics.

We can define Class, Method, Delegate, Interface and Events as Generics. Going further we will concentrate on this.

  1. Generic Class: The best part of generics is its ability and its usage. Below I have defined a generic class that allows me to pass any type in the constructor.
    1. //We have defined class as Generic type  
    2. publicclassGenericClassExample < T >  
    3. {  
    4.     T AnyVariable;  
    5.     //We can pass any type in constructor by defining class as generic <T>  
    6.     GenericClassExample(T anyVariable)  
    7.     {  
    8.         AnyVariable = anyVariable;  
    9.     }  
    10.     publicvoidPrintAnyVariable()  
    11.     {  
    12.         Console.WriteLine("Any Variable " + AnyVariable);  
    13.     }  
    14. }  
  2. Generic Method: Same as the class, a method can also be declared as Generic type. Please find below the example for the same:
    1. public class GenericMethodExample  
    2. {  
    3.     public void GenericMethod < T > (T a, T b)  
    4.     {  
    5.         T val1, val2;  
    6.         val1 = a;  
    7.         val2 = b;  
    8.     }  
    9. }  
  3. Generic Interface: Below is an example for the same:
    1. namespace Project1  
    2. {  
    3.     public interface InterfaceGeneric < T >  
    4.     {  
    5.         void ShowNumber(T item);  
    6.     }  
    7. }  
    8. class Interface < T >: InterfaceGeneric < T >  
    9. {  
    10.     public void ShowNumber(T item)  
    11.     {  
    12.         Console.WriteLine(item);  
    13.         Console.ReadKey();  
    14.     }  
    15. }  
    16. class test  
    17. {  
    18.     public static void Main(string[] args)  
    19.     {  
    20.         Interface < int > t1 = newInterface < int > ();  
    21.         Interface < string > t2 = newInterface < string > ();  
    22.         t1.ShowNumber(4);  
    23.         t2.ShowNumber("shubham");  
    24.     }  
    25. }  
  4. Generic Delegates: Here you go.
    1. public class Program  
    2. {  
    3.     delegate T GenericDel < T > (T n1, T n2);  
    4.     static void Main(string[] args)  
    5.     {  
    6.         GenericDel < int > objDelegate = newGenericDel < int > (Add);  
    7.         GenericDel < int > objDelegateMul = newGenericDel < int > (Multiply);  
    8.         Console.WriteLine(objDelegate(1, 2));  
    9.         Console.WriteLine(objDelegateMul(1, 2));  
    10.         Console.ReadLine();  
    11.     }  
    12.     public static int Add(int a, int b)  
    13.     {  
    14.         return a + b;  
    15.     }  
    16.     public static int Multiply(int a, int b)  
    17.     {  
    18.         return a * b;  
    19.     }  
    20. }  

Hope this will make the concept of <T> clear to the readers. And event is homework for you all.

Up Next
    Ebook Download
    View all
    Learn
    View all