Generics are simply placeholders for actual types. Generics are defined with left and right brackets: <T> In other words Generics allow you to define type-safe data structures, without committing to actual data types. .Net 2.0 provides number of generic collection classes for lists, stacks, queues, dictionaries.
Generics Benefits:
Generics in .NET let you reuse code and the effort you put into implementing it. The types and internal data can change without causing code bloat, regardless of whether you are using value or reference types. You can develop, test, and deploy your code once, reuse it with any type, including future types, all with full compiler support and type safety. Because the generic code does not force the boxing and unboxing of value types, or the down casting of reference types, performance is greatly improved. With value types there is typically a 200 percent performance gain, and with reference types you can expect up to a 100 percent performance gain in accessing the type (of course, the application as a whole may or may not experience any performance improvements). The source code available with this article includes a micro-benchmark application, which executes a stack in a tight loop. The application lets you experiment with value and reference types on an Object-based stack and a generic stack, as well as changing the number of loop iterations to see the effect generics have on performance.
Let us create console application to have actual understanding.
In visual studio 2005 create new project >>Console Application Name it as "GenericsLists".
We will add class called as "Customer" to this console application as below:
public class Customer
{
private int custId;
public Customer(int Id)
{
this.custId = Id;
}
public override string ToString()
{
return custId.ToString();
}
}
With any of the console application we will have something called as "Program.cs"
Which will be some thing like this:
public class Program
{
static void Main()
{
//Type safe list (of Customer objects)
List<Customer> custList = new List<Customer>();
//Type safe list (of integers)
List<int> intList = new List<int>();
//Populate the Lists
for (int i = 0; i < 5; i++)

Shankar ReshmimathPosted Mar 9, 2011, 9:55 AM
Hi Munir Shaikh, It is a good article for beginners .. I am facing some problems with the below Generics problem which i wrote.. Please go through it and correct the program .. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace GenericExam2 { class simple<T> { public T Add(T a, T b) { T c; c = a + b; return c; } public T Mult(T a, T b) { T c = a * b; return c; } public T Add(T a, T b, T c) { T d = a + b + c; return d; } } class Program { static void Main(string[] args) { simple<int> s1 = new simple<int>(); simple<double> s2 = new simple<double>(); int i1 = s1.Add(10, 20); double d1 = s2.Add(10.5, 10.5); int i2 = s1.Add(10, 20, 30); double d2 = s2.Add(3.5, 3.5, 3.5); int i3 = s1.Mult(10, 20); double d3 = s2.Mult(2.5, 2.5); Console.WriteLine(" \n Addition of two integers : {0}", i1); Console.WriteLine(" \n Addition of three integers : {0}", i2); Console.WriteLine(" \n Multiplication of two integers : {0}", i3); Console.WriteLine(" \n Addition of two doubles : {0}", d1); Console.WriteLine(" \n Addition of three doubles : {0}", d2); Console.WriteLine(" \n Multiplication of two doubles : {0}", d3); Console.ReadLine(); } } } I am getting error message like : " Error 1 Operator '+' cannot be applied to operands of type 'T' and 'T' " ... please help with this ... Thank you, Shankar Reshmimath
vipula RaoPosted Sep 20, 2007, 6:36 AM
Respected Sir, In my project i have to show if i enter a number in one textbox then that number multiply with 7 result will show in another text box . sir plz urgent
vipula RaoPosted Sep 20, 2007, 6:34 AM
Respected Sir, In my project i have to show if i enter a number in one textbox then that number multiply with 7 result will show in another text box . sir plz urgent
abc defPosted Aug 30, 2007, 4:11 AM
shuaxin yemian de shihou hai neng tijiao ? wo kao ,zuo de ye tai laji le .
abc defPosted Aug 30, 2007, 4:10 AM
shuaxin yemian de shihou hai neng tijiao ? wo kao ,zuo de ye tai laji le .
abc defPosted Aug 30, 2007, 4:10 AM
ke xi an de yingwen bu hao !!BS nimen zhexie lao wai
abc defPosted Aug 30, 2007, 3:55 AM
ke xi an de yingwen bu hao !!BS nimen zhexie lao wai
abc defPosted Aug 30, 2007, 3:55 AM
ke xi an de yingwen bu hao !!BS nimen zhexie lao wai
abc defPosted Aug 30, 2007, 3:54 AM
???,???????