Thursday, August 9, 2007

Generics > Code Listing

CODE LISTING > GENERIC
public class Test< T >
{
public void Display()
{

}
}

At instantiation of this class, the generic has to be bound to any specific type:
Test< int > objTest = new Test< int >();

After instantiation, the object objText is a bound type.

IMPLEMENTING A GENERIC METHOD
using System;
using System.Collections.Generic;

class Test
{
static void Main( string[] args )
{
Test t = new Test();
int[] integerArray = {1,2,3,4,5,6};
char[] characterArray = { 'J', 'O', 'Y', 'D', 'I','P' };
double[] doubleArray = {0.1,0.2,0.3,0.4,0.5,0.6};
Console.WriteLine( "Displaying the contents of the integer array:--" );
t.Display(integerArray);
Console.WriteLine( "Displaying the contents of the character array:--" );
t.Display(characterArray);
Console.WriteLine( "Displaying the contents of the double array:--" );
t.Display(doubleArray);
}

public void Display< GenericArray >( GenericArray[] array )
{
for (int i = 0; i< array.Length; i++)
Console.WriteLine(array[i]);
}
}

No comments: