Saturday, April 5, 2014

Generic in c#

Generics are introduced in C# 2.0. Basically generics allow you to write a class or method that can work on any data type. Generic classes are extensively used by collection classes available in System.Collections.Generic namespace

Example. 

Class program
{
static void Swap<T> (ref T lhs, ref T rhs)
{
//code here
}
Main()
{
swap<int>(ref a, ref b); // Note- you can declare the type when you create instance or call the method
}
}

No comments:

Post a Comment