Posts

Showing posts from October, 2013

C# - Generics

Generics allow you to delay the specification of the data type of programming elements in a class or a method, until it is actually used in the program. In other words, generics allow you to write a class or method that can work with any data type. You write the specifications for the class or the method, with substitute parameters for data types. When the compiler encounters a constructor for the class or a function call for the method, it generates code to handle the specific data type. A simple example would help understanding the concept: using System; using System.Collections.Generic; namespace GenericApplication {     public class MyGenericArray<T>     {         private T[] array;         public MyGenericArray(int size)         {             array = new T[size + 1];         }         public T getItem(int index)         {             return array[index];         }         public void setItem(int index, T value)         {             array[index] = valu