Search This Blog

Thursday, November 21, 2013

Generics C#

C# > Generics

Generics means the concept of type parameters. In this way it is possible to design classes  that defer the specification of types until the class is declared and instantiated.
  • generic types maximize code reuse, type safety, and performance.
  • T is generic type parameter
Links                                                                                                  

Example: Simple generic list                                                           

public class GenericList<T>
{
    private List<T> l = new List<T>();
    public void Add(T element)
    {
       l.Add(element);
    }
}

GenericList<int> lst1 = new GenericList<int>();
lst1.Add(1);

GenericList<string> lst2 = new GenericList<string>();
lst2.Add("generics");