luni, 25 august 2014

Generics C#

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

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");


Links                                                                                                  

Niciun comentariu:

Trimiteți un comentariu