Programming in almost language

This is the site where you may share your knowledge and experience to eachother..

  • Categories

  • LinkedIn

  • Tweet Me

  • My footsteps

Generic Class

Posted by Praveen Kumar on March 20, 2008

This articles that shows how to declare and use generic methods in the C# programming language. In this article, i introduce the concept of generics.I will explain you an example using overloaded methods to motivate the need for generic methods. we reimplement the overloaded methods from the example using a single generic method.

Generic classes enable you to specify, with a single class declaration, a set of related classes.

Generic methods enable you to specify, with a single method declaration, a set of related methods.

Similarly, generic interfaces enable you to specify, with a single interface declaration, a set of related interfaces.

Generics provide compile-time type safety. [Note: You can also implement generic structs and delegates.]

We can write a generic method for sorting an array of objects, then invoke the generic method separately with an int array, a double array, a string array and so on, to sort each different type of array.

The compiler performs type checking to ensure that the array passed to the sorting method contains only elements of the same type.

We can write a single generic Stack class that manipulates a stack of objects, then instantiate Stackints, a stack of doubles, a stack of strings and so on. The compiler performs type checking to ensure that the Stack stores only elements of the same type.

I will explain examples of generic methods and generic classes.

How to declare and use Generics Method

I present an example using overloaded methods to motivate the need for generic methods.

I reimplement the overloaded methods from the example in this article using a single generic method.

Overloaded methods are often used to perform similar operations on different types of data. The below example contains three overloaded DisplayArray method(color in red).

These methods display the elements of an int array, a double array and a char array, respectively. Soon, we will reimplement this program more concisely and elegantly using a single generic method.

using System;
class OverloadedMethods
{
static void Main( string[] args )
{
// create arrays of int, double and char
int[] intArray = { 1, 2, 3, 4, 5, 6 };
double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };
char[] charArray = { ‘H’, ‘E’, ‘L’, ‘L’, ‘O’ };

Console.WriteLine( “Array intArray contains:” );
DisplayArray( intArray ); // pass an int array argument
Console.WriteLine( “Array doubleArray contains:” );
DisplayArray( doubleArray ); // pass a double array argument
Console.WriteLine( “Array charArray contains:” );
DisplayArray( charArray ); // pass a char array argument
} // end Main
// output int array

static void DisplayArray( int[] inputArray )
{
foreach ( int element in inputArray )
Console.Write( element + ” ” );
Console.WriteLine( “\n” );
} // end method DisplayArray
// output double array

static void DisplayArray( double[] inputArray )
{
foreach ( double element in inputArray )
Console.Write( element + ” ” );
Console.WriteLine( “\n” );
} // end method DisplayArray
// output char array

static void DisplayArray( char[] inputArray )
{
foreach ( char element in inputArray )
Console.Write( element + ” ” );
Console.WriteLine( “\n” );
} // end method DisplayArray
} // end class OverloadedMethods

OUTPUT

Array intArray contains:
1 2 3 4 5 6

Array doubleArray contains:
1.1 2.2 3.3 4.4 5.5 6.6 7.7 

Array charArray contains:
H E L L O

Generic Method Implementation

If the operations performed by several overloaded methods are identical for each argument type, the overloaded methods can be more compactly and conveniently coded using a generic method.

You can write a single generic method declaration that can be called at different times with arguments of different types. Based on the types of the arguments passed to the generic method, the compiler handles each method call appropriately.

All generic method declarations have a type parameter list delimited by angle brackets (< E > in this example) that follows the method’s name.

using System;
using System.Collections.Generic;

class GenericMethod
{
static void Main( string[] args )
{
// create arrays of int, double and char
int[] intArray = { 1, 2, 3, 4, 5, 6 };
double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };
char[] charArray = { ‘H’, ‘E’, ‘L’, ‘L’, ‘O’ };

Console.WriteLine( “Array intArray contains:” );
DisplayArray( intArray ); // pass an int array argument
Console.WriteLine( “Array doubleArray contains:” );
DisplayArray( doubleArray ); // pass a double array argument
Console.WriteLine( “Array charArray contains:” );
DisplayArray( charArray ); // pass a char array argument
} // end Main

// output array of all types
static void DisplayArray< E >( E[] inputArray )
{
foreach ( E element in inputArray )
Console.Write( element + ” ” );
Console.WriteLine( “\n” );
} // end method PrintArray
} // end class GenericMethod

Array intArray contains:
1 2 3 4 5 6

Array doubleArray contains:
1.1 2.2 3.3 4.4 5.5 6.6 7.7 

Array charArray contains:
H E L L O

Each type parameter list contains one or more type parameters, separated by commas.

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 parameters act as placeholders for the types of the arguments passed to the generic method.

A generic method’s body is declared like that of any other method.

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

For example, element in the foreach statement as type E, which matches the type parameter (E) declared.

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.

Common Programming Error:

  • If you forget to include the type parameter list when declaring a generic method, the compiler will not recognize the type parameter names when they are encountered in the method. This results in compilation errors.
  • It is recommended that type parameters be specified as individual capital letters. Typically, a type parameter that represents the type of an element in an array (or other collection) is named E for “element” or T for “type.”

Advertisements

One Response to “Generic Class”

  1. Thank you !

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

 
%d bloggers like this: