.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Generic Class

Posted By:Deepika Haridas       Posted Date: September 25, 2009    Points: 25    Category: C#    URL: http://www.dotnetspark.com  
 

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.

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.


 Subscribe to Articles

     

Further Readings:

Responses
Author: Ramesh         Company URL: http://www.dotnetspark.com
Posted Date: September 28, 2009

Good One Ma''am...Nice and simple explanation about Generics....

Regards
Ramesh
Author: Deepika Haridas         Company URL: http://www.dotnetspark.com
Posted Date: September 29, 2009

Thank you

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend