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.
    //We have defined class as Generic type  
    public class GenericClassExample < T >  
    {  
        T AnyVariable;  
        //We can pass any type in constructor by defining class as generic <T>  
        GenericClassExample(T anyVariable)  
        {  
            AnyVariable = anyVariable;  
        }  
        public void PrintAnyVariable()  
        {  
            Console.WriteLine("Any Variable " + AnyVariable);  
        }  
    }
  2. Generic Method: Same as the class, a method can also be declared as Generic type. Please find below the example for the same:
    public class GenericMethodExample  
    {  
        public void GenericMethod < T > (T a, T b)  
        {  
            T val1, val2;  
            val1 = a;  
            val2 = b;  
        }  
    }
  3. Generic Interface: Below is an example for the same:
    namespace Project1  
    {  
        public interface InterfaceGeneric < T >  
        {  
            void ShowNumber(T item);  
        }  
    }  
    class Interface < T >: InterfaceGeneric < T >  
    {  
        public void ShowNumber(T item)  
        {  
            Console.WriteLine(item);  
            Console.ReadKey();  
        }  
    }  
    class test  
    {  
        public static void Main(string[] args)  
        {  
            Interface < int > t1 = newInterface < int > ();  
            Interface < string > t2 = newInterface < string > ();  
            t1.ShowNumber(4);  
            t2.ShowNumber("shubham");  
        }  
    }
  4. Generic Delegates: Here you go.
    public class Program  
    {  
        delegate T GenericDel < T > (T n1, T n2);  
        static void Main(string[] args)  
        {  
            GenericDel < int > objDelegate = newGenericDel < int > (Add);  
            GenericDel < int > objDelegateMul = newGenericDel < int > (Multiply);  
            Console.WriteLine(objDelegate(1, 2));  
            Console.WriteLine(objDelegateMul(1, 2));  
            Console.ReadLine();  
        }  
        public static int Add(int a, int b)  
        {  
            return a + b;  
        }  
        public static int Multiply(int a, int b)  
        {  
            return a * b;  
        }  
    }

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


Recommended Free Ebook
Similar Articles