Generics – Method Declarations

Generic methods are used in situations where we perform similar operations on different types of data. In normal scenario this is done by using function overriding and creating separate functions for each type.

Let us have a look at an example, which prints the array of  different types :

public void PrintArray(int[] inputArray)
{
foreach(int input in inputArray)
{
Console.Write(input +  ”  ” );
}

}

.. for float types we will have to override the function as follows,

public void PrintArray(float[] inputArray)
{
foreach(float input in inputArray)
{
Console.Write(input +  ”  ” );
}
}

.. for double types we will have to override the function as follows,

public void PrintArray(double[] inputArray)
{
foreach(double input in inputArray)
{
Console.Write(input +  ”  ” );
}
}

The function will be called as follows :

PrintArray(integerArray);

PrintArray(floatArray);

PrintArray(doubleArray);

So we can see that we now have three implementations of same function for different types.  This can be solved by the help of Generic methods. Using Generic methods we can create one single function and pass the required types as parameters to this function.

Let us see how this is done:

public void PrintArray<E>(E[] inputArray)
{
foreach(E input in inputArray)
{
Console.Write(input  + ” “);
}
}

where ‘E’ represents the type of array that we will be passing to the function. The function will be called in this way, with different types :

PrintArray<int>(integerArray);

PrintArray<float>(floatArray);

PrintArray<double>(doubleArray);


Generic declaration
A generic method is declared in the same way as a normal function, except that, the actual parameter types, are replaced with  placeholders called as “type parameter”.

Eg : Instead of

PrintArray(int[] integerArray)
{
– – – – –      ;
– – – – –      ;
– – – – –      ;
}

we write

PrintArray(E[] inputArray)
{
– – – – –      ;
– – – – –      ;
– – – – –      ;
}

Now, before specifying the type parameter, we need to create a type parameter list in the function declaration.  The type parameter list is a list of type parameter that we will be using in the generic function.  Type argument list can be considered as  a way of  announcing in advance that these will be the placeholders we will be using in the function, so do not give compilation errors when you encounter them.

The type parameter list is declared as follows:

PrintArray<E>(E[] inputArray)
{
– – – – ;
– – – – ;
}

The <E> portion in the above declaration is the type parameter list.  The type parameter list is delimited by the angular brackets and follows the method’s name.  Each type parameter list contains one or more type parameters, separated by commas.

As mentioned before, a type parameter is an identifier that is used in place of actual type names. The type parameters can be used to declare the return type, the parameter types and the local variable types in a generic method declaration.

The type parameter names throughout the method declaration must match those declared in the type parameter list.

Also, a type parameter can be declared only once in the type parameter list but can appear more than once in the method’s parameter list. Type parameter names need not be unique among different generic methods.

A generic method having multiple arguments is declared as follows:

void PrintKeyAndValue<TKey, TValue>(TKey key, TValue value)
{

Console.Write(“Key :  “);
Console.WriteLine(key.ToString());

Console.Write(“Value :  “);
Console.WriteLine(value.ToString());
}

A generic method having a return type is declared as follows:

TValue returnPassedArgument<TValue>(TValue value)
{

return value;

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s