Generics is a new feature which is introduce in .Net version 2.0 and the common language runtime (CLR) When you use generics, you are creating classes or methods that use a generic type, rather than a specific type. For example, rather than creating a type-specific, you could create a reusable List class using generics.
Generics work a bit differently for reference types. The first time a generic type is constructed with any reference type, the runtime creates a specialized generic type with the object references substituted for the parameters in the MSIL. Then, each time a constructed type is instantiated with a reference type as its parameter, regardless of its type, the runtime reuses the previously created specialized version of the generic type. This is possible because all references are the same size.
For example, suppose you had two reference types, a Student
class and an Employee
class, and that you created a GenericCollection of Customer
types:
GenericCollection<student> students = new GenericCollection<student>();
GenericCollection<Employee> Employess = new GenericCollection<Employee>();
Below is the example of Generics.
Class
Let us create a sample class to get a feel of this. We will take the example of the common GenericCollection class. The class will hold Add and Remove and Item methods. While implementing, we can specify which data type the object can hold.
public class GenericCollection
{
public void Add(T GenericObject)
{
InnerList.Add(GenericObject);}
public void Remove(int index)
{
InnerList.RemoveAt(index);}
public T Item(int index)
{
return (T)InnerList[index];}
}
Consider the class Student
public class student
{
private string Strfname;
private string Strlname;
private int Intage;
public student(string fname, string lname, int age)
{
Strfname = fname;
Strlname = lname;
Intage = age;
}
public new string ToString()
{
return ("First Name : " + Strfname + "" +
"Last Name : " + Strlname +
"Age : " + Intage);
}
}
Consider the class Employee
public class Employee
{
private string strEmpName;
private int IntEmpID;
private int intempAge;
public Employee(string EmployeeName, int EmployeeID, Int32 Age)
{
strEmpName = EmployeeName;
IntEmpID = EmployeeID;
intempAge = Age;
}
public new string ToString()
{
return ("Name : " + strEmpName + "" +
"ID : " + IntEmpID + "" +
"Age : " + intempAge.ToString());
}
}
So now that we have a Type-safe list of our Student and Employee classes, let's see how we can use these classes. What we will do is simply creates object and add that Student and Employee to our GenericCollection
GenericCollection<student> students = new GenericCollection<student>();
GenericCollection<Employee> Employess = new GenericCollection<Employee>();
students.Add(new student("Trisha", "Sharma", 22));
students.Add(new student("Vinay", "Kapoor", 32));
students.Add(new student("Rohit", "Khanna", 27));
Employess.Add(new Employee("Rahul", 1001, 28));
Employess.Add(new Employee("David", 1002, 32));
Response.Write("Customers");
Response.Write("-------------------------------");
foreach (student Stud in students)
{
Response.Write(""+Stud.ToString());
}
Response.Write("Employees");
Response.Write("-------------------------------");
foreach (Employee emp in Employess)
{
Response.Write(""+emp.ToString());
}
Output
Customers
-------------------------------
First Name : Trisha
Last Name : Sharma
Age : 22
First Name : Vinay
Last Name : Kapoor
Age : 32
First Name : Rohit
Last Name : Khanna
Age : 27
Employees
-------------------------------
Name : Rahul
ID : 1001
Age : 28
Name : David
ID : 1002
Age : 32
Generics are a great way of writing classes that combine reusability, type safety and efficiency. Generics are commonly used with collections. .NET 2.0 has introduced a new namespace called System.Collections.Generic which contains classes that support generics.
Comments
Post a Comment