Concept of Type parameter with the help of Generics

Hi

Generics are the new feature in the c# 2.0. They introduce the concept of Type parameter. This makes it possible to classes and methods that differs the specification of one or more types until the class or method is declared and used by the code, which will be using it.

Generic helps us to maximize the reuse of code, Type safety and better performance. Generics are most commonly used to create a collection. There are many new generic collection classes in the System.collection.Generics namespace. These classes are advised to be used for type safety and better performance over araylist.

We can also create our own Generic interface, classes, methods, events and delegates. Information on the types used in a generic data type may be obtained at run-time by means of reflection

We can declare a generic class like this

public class GenericClass<T>
{

    void Add(T input) { }

}

We create an instance of the generic class like this

   GenericClass<string> list2 = new GenericClass<string>();

Here if want we can also initialize the class to be of any other type like a user defined class. For example

Public class test
{

Pubic class TestClass{}

   GenericClass<TestClass> list2 = new GenericClass<TestClass>();

}

Hope this helps
Thanks
Vikram


Share this post   Email it

Feedback

Posted on 10/9/2007 3:06:27 AM

very good stuff

Posted on 3/17/2008 6:20:21 AM

Also give information about Generic functions and properties

Posted on 4/7/2009 6:10:08 AM

Really Helpfull but please elaborate its type and methods.

Posted on 2/23/2010 2:02:09 PM

i think it would be more beneficial if u provide some practical examples as well..however it helps to some extent in understanding the basic concept of Generics...thank u

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Copyright © 2006 - 2010 Vikram Lakhotia