Introduction
Generics gives you the ability to create a generic methods or a generic type by defining a placeholder for method arguments or type definitions, which are specified at the time of invoking the generic method or creating the generic type.
Generic classes and structures
A generic class is a class that have a type parameter <T> representing the data type of the class variables.
public class MyGenericClass<T>
{
//Generic variables
private T d1;
private T d2;
private T d3;
public MyGenericClass(){}
//Generic constructor
public MyGenericClass(T a1, T a2, a3)
{
d1 = a1;
d2 = a2;
d3 = a3;
}
//Generic properties
public T D1
{
get{return d1; }
set{d1 = value; }
}
public T D2
{
get{ return d2; }
set{ d2 = value; }
}
public T D3
{
get{ return d3; }
set{ d3 = value; }
}
}
Assume you want a way to reset the variables in your generic class to its default values, but in fact you don't know really what is the data type that the placeholder <T> will represent, so you can't know what is the default value will be.
In generic classes you can make use of the default keyword to reset your data safely to its default value as follow:
d1 = default(T);
So whatever your type parameter will be, you can reset it safely to its default value.
We can add this method to our generic class:
public void ResetValues()
{
d1 = default(T);
d2 = default(T);
d3 = default(T);
}
Here is our generic class in use:
//contain int values
MyGenericClass<int> c1 = new MyGenericClass<int>(5, 10, 15);
//reset values to 0
c1.ResetValues();
//contain string values
MyGenericClass<string> c2 = new MyGenericClass<string>("a", "b", "c");
//reset values to null
c2.ResetValues();
Note: you can create generic structures in the same way of creating generic classes, you only have to remove the class keyword and add struct keyword.
public struct MyGenericStruct<T>
{
}
Creating a Custom Generic Collection
Under the namespace System.Collections.Generic you will find a lot of generic collections types and maybe you will not need to build a custom generic collection.
Building a custom generic collection allows you to build a generic collection that have your own methods and you can also constrain you generic collection to contain only the data type you want.
A generic collection is a generic class that implements the IEnumerable<T> generic interface. The IEnumerable<T> interface allows you to support foreach loop.
public class EmployeeCollection<T> : IEnumerable<T>
{
List<T> empList = new List<T>();
public void AddEmployee(T e)
{
empList.Add(e);
}
public T GetEmployee(int index)
{
return empList[index];
}
//Compile time Error
public void PrintEmployeeData(int index)
{
Console.WriteLine(empList[index].EmployeeData);
}
//foreach support
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return empList.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return empList.GetEnumerator();
}
}
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 string EmployeeData
{
get {return String.Format("{0} {1} is {2} years old", FirstName, LastName, Age); }
}
}

dani crisPosted Jun 4, 2015, 9:54 AM
muy util. gracias
Atieq ur RehmanPosted Apr 22, 2015, 12:01 PM
good one :)
Guest UserPosted Sep 4, 2014, 7:17 AM
i liked examples
avishek chowdhuryPosted Dec 21, 2011, 12:31 AM
Till now,generic was some unknown demon for me,Thanks to this article,I am confident in facing any question in interview related to generics. Thank you very much Amr Monjid! God bless you!
Shankar ReshmimathPosted Mar 8, 2011, 1:54 PM
Hi Amr Monjid , It is a good article to understand the Generics i.e parametric polymorphism .. I am facing some problems with the below program which I wrote .. please go through it and reply me .. 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 Muliplication 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 Muliplication of two doubles : {0}", d3); Console.ReadLine(); } } } Please check the above program and reply me sir .... Thanking you, Shankar Reshmimath
Ashwini varmaPosted Dec 8, 2010, 1:24 PM
It is a vry good post and it vry useful who are beginners of generics
mohammad hassan mohammadiPosted Oct 7, 2010, 3:10 AM
thanks of your of your help