Tuesday, December 16, 2014

Understanding Generics


Understanding 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.




namespace Apps
{
    class UnderstandingGenerics
    {
        static void GenericsTest<T>(T lhs, T rhs)
        {
            Console.WriteLine("{0}, {1}", lhs, rhs);
        }
        static void Main(string[] args)
        {
            int a = 10, b = 2;
            Char c = 'r', d = 's';
            GenericsTest<int>(a, b);
            GenericsTest<char>(c, d);
        }
    }
}