Introduction
Generics give you the ability to create generic methods or a generic type by defining a placeholder for method arguments or type definitions, that are specified at the time of invoking the generic method or creating the generic type.
Why Generics?
To understand the importance of generics let's start by seeing some kind of problems that can be solved by them.
ArrayList
Let's start by creating an ArrayList to hold stack-allocated data.
ArrayList intList = new ArrayList();
As you may know, the ArrayList collection can receive and return only an Object type, so the runtime will convert the value type (stack-allocated) automatically via boxing operation into an Object (heap-allocated) as in the following:
ArrayList intList = new ArrayList();
//add a value type to the collection(boxing)
intList.Add(5);
To retrieve the value from the ArrayList you must unbox the heap-allocated object into a stack-allocated integer using a casting operation.
//unboxing
int x = (int)intList[0];
The problem with the stack/heap memory transfer is that it can have a big impact on performance of your application because when you use boxing and unboxing operations the following steps occur:
- A new object must be allocated on the managed heap.
- The value of the stack-allocated type must be transferred into that memory location.
- When unboxed, the value stored on the heap must be transferred back to the stack.
- The unused object on the heap will be garbage collected.
Now consider that your ArrayList contained thousands of integers that are manipulated by your program, this for sure will have an affect on your application performance.
Custom Collections
Assume that you need to create a custom collection that can only contain objects of the Employee type.
public class Employee
{
string FirstName;
string LastName;
int Age;
public Employee(){}
public Employee(string fName, string lName, int Age)
{
this.Age = Age;
this.FirstName = fName;
this.Lastname = lName;
}
public override string ToString()
{
return String.Format("{0} {1} is {2} years old", FirstName, LastName, Age);
}
}
Now we will build the custom collection as in the following:
public class EmployeesCollection : IEnumerable
{
ArrayList alEmployees = new ArrayList();
public EmployeesCollection() { }
//Insert Employee type
public void AddEmployee(Employee e)
{
//boxing
alEmployees.Add(e);
}
//get the employee type
public Employee GetEmployee(int index)
{
//unboxing
return (Employee)alEmployees[index];
}
//to use foreach
IEnumerator IEnumerable.GetEnumerator()
{
return alEmployees.GetEnumerator();
}
}
The problem here is that if you have many types in you application then you need to create multiple custom collections, one for each type. And as you can see, we also have the problem of boxing and unboxing here.
All problems you saw previously can be solved using generics, so let's see what we can do.
The System.Collections.generic namespace
You can find many generic collections in the System.Collections.Generic just like:

Mohammad KhalidPosted Sep 12, 2014, 1:59 AM
Article is nice and I find it useful for developers.
Guest UserPosted Sep 4, 2014, 7:17 AM
i bookmarked this nice article
MehdiPosted Aug 7, 2013, 2:35 AM
Very nice, thanks
BadiyaPosted Sep 25, 2011, 8:29 AM
It was very useful for me.
venkat tadivakaPosted Mar 18, 2011, 1:36 PM
h
Rahul Pushpendu BhaskarPosted Nov 15, 2010, 6:45 AM
thank u lot.....
Mohammed MostafaPosted May 6, 2010, 11:39 PM
Your articles here are so great, Thanks alot
SenthilkumarPosted Aug 6, 2009, 1:15 AM
Hi Amr Monjid, You have said like if we store 1000 int items on the stack and while retrieving the items we have to do the unboxing. It will affect the performance. Yes! its right!! Here i would like to add one more important use of Generics. Consider a scenario like if we are going to store the some integer value in the stack. Then we are going to iterate the stack and add the value. For that we have to unboxing, that time of there is some string values there. That time it will throw the type casting error. Its run time error. This error was unavoidable up to .Net 1.1. This issue was rectified in the .Net 2.0. If you use the Generics then it will give the compile time error instead of run time error. Thank you Erode Senthilkumar
SenthilkumarPosted Aug 6, 2009, 12:45 AM
Hey Amr Monjid, Your article looking pretty good. I have one doubt. If you stored an object in the ArrayList then it will be stored on the heap memory. If you do unboxing then it needs to be converted into value type. Of course the value type will be stored on the stack. But in this scenario, after unboxing the data will be there in same position as it is in the heap memory.But it will add the memory location address to the stack. Please verify that. Thanks. Erode Senthilkumar