Generic is a new feature in C#
2.0.generic classes allow programmers to create classes support for
any type. When we instantiate we can specify a specific type for
each instance.
Generic Class Example:-
Let’s look at an example for generic class
Following is my generic class which
allow client program to create stack type collections using different type of
objects
class
Stack<T>
{
public List<T> itemList = new List<T>();
public void Push(T item)
{
itemList.Add(item);
}
public T Pop()
{
T item= itemList[itemList.Count - 1];
itemList.Remove(item);
return item;
}
public void ShowItems()
{
for (int i =itemList.Count-1; i >=0; i--)
{
Console.WriteLine(itemList[i]);
}
}
}
Following is my client program it create objects of that Stack class and each object treat the class with different type of objects
static void
Main(string[] args)
{
Stack<string> st = new Stack<string>();//stack
with string elements
st.Push("item1");
st.Push("item2");
st.Push("item3");
st.Pop();
st.ShowItems();
Console.WriteLine("------------------------------------------");
Stack<int> st2 = new Stack<int>();//stack with
int elements
st2.Push(1);
st2.Push(2);
st2.Push(3);
st2.Pop();
st2.ShowItems();
Console.Read();
}
Generic class can create instant
for specific type. So programmers can reuse that class for various data types.
Generic Method Example:-
Normally when you work with large scale applications you will have to retrieve different type of data set from your data base. So you may follow the following sequence if you use three tiered architecture.
Client
Layer à
Business Layer à
Data Layer
As an example suppose you need to retrieve student List, Course List, Account Data List and Facility List from your data base during the application. So you can write simple one method in your business layer which will pass stored procedure name to data layer.
So then data layer will execute
specified stored procedure and retrieve the set of data from data base and
convert it to relevant list.
public List<T> GetDataList(string storedProcedure)
{
return (dataLayer.GetData(storedProcedure);
}
From your client code you can call this method as following
BusinessLayer.GetDataList<Student>("SP_GET_STUDENT_DATA");
BusinessLayer.GetDataList<Course>("SP_GET_COURSE_DATA");
Generics with multiple types
You can create generic methods (even
classes and other generic members) with more than one types.
Following is my generic class which use
2 types,
class CustomeType<T,
U>
{
private T
identity;
private U
marks;
private Dictionary<T, U> dictionary = new Dictionary<T, U>();
public T
Identity
{
get { return this.identity; }
set {
identity = value; }
}
public U
Marks
{
get { return this.marks; }
set {
marks = value; }
}
}
Here is the way I’m going to use this
class,
CustomeType<string, string> nursaryStudent = new CustomeType<string, string>();
nursaryStudent.Identity = "Mala";
nursaryStudent.Marks = "Very Good";
Console.WriteLine(string.Format("Name={0}
Level={1}", nursaryStudent.Identity,
nursaryStudent.Marks));
CustomeType<int, int> SchoolStudent = new CustomeType<int, int>();
SchoolStudent.Identity = 201;
SchoolStudent.Marks = 80;
Console.WriteLine(string.Format("Student
Id={0} Marks={1}", SchoolStudent.Identity,
SchoolStudent.Marks));
CustomeType<int, decimal> UniversityStudent = new CustomeType<int, decimal>();
UniversityStudent.Identity = 20;
UniversityStudent.Marks = 3.7M;
Console.WriteLine(string.Format("UniStuId={0}
GPA={1}", UniversityStudent.Identity,
UniversityStudent.Marks));
Generic Method With multiple types
Following method return type and one
parameter type are set as generic types
public T1
GetData<T1,T2>(T2 param, string storedProcedure)
{
T1 returnVal= DataLayer.GetData(param, storedProcedure);
return returnVal;
}
And remember that, we can also create
generic interfaces, Generics delegates, Generics Structs in C#.NET. Try to do
more examples to familiar with this valuable concept