Search this blog

You can search for all the topics in our blog using our google custom search now.

Search the blog

Loading

Saturday, May 22, 2010

Generics V/S Non Generics

Why use Generics??
1.By using generics we can ensure appropriate use of types
2.Improves performance by reducing the need to cast.

Non Generic Classes

I will explain now how Non generic classes differ from Generic classes.

Let us now compare SortedList from Non generic classes and SortedList from Generic Class to understand how does generics actually ensure the appropriate use of types.Lets create a class called Company as follows:

public class Company
{
public SortedList EmpList = new SortedList();

public Company()
{
EmpList.Add("Sameer", 5000);
EmpList.Add("Sanjay", 2000);
EmpList.Add("Shraddha", 1000);



}


}

Am Creating a sorted list which stores "Names" and "Salary" as Key/Value Pair. So we add a few pairs into the sorted list. Lets create another class as follows:

class Program
{


static void Main(string[] args)
{

Company Acompany = new Company();

foreach (string salary in Acompany.EmpList.Values)
{
Console.WriteLine(salary);
/*If you check EmpList has "Salary" of type Int. Hence the above line will require to cast the Int Object to be converted to String Object.This code wont give you any error even if no explicit type casting is mentioned.This kind of conversion hits performance.And also in many cases you will get many errors while casting between different types*/

}
Console.ReadKey();




}
The Above class displays the salaries of employees using Non Generic Class.

USAGE OF GENERICS
Lets Create another class called CompanyGeneric, which uses a Generic Sorted List as follows:

public class CompanyGeneric
{
public SortedList BEmployeeList = new SortedList();

public CompanyGeneric()
{

BEmployeeList.Add("Sameer", 5000);
BEmployeeList.Add("Sanjay", 2000);
BEmployeeList.Add("Shraddha", 1000);

}

}

Now let me display the salaries of all the employees like i did with sorted list.

class Program
{


static void Main(string[] args)
{


CompanyGeneric BCompany = new CompanyGeneric();


foreach (string s in BCompany.BEmployeeList.Values)
{

Console.WriteLine(s);
/*Now when you try to execute this code, you will get an error saying "Cannot convert type int to string". This is how generics helps us in ensuring appropriate use of types and improving the system's performance by reducing the casting operation.If we didn't have an option of generics, our code will be more error prone and tedious.*/

}
Console.ReadKey();


You have to change the above code as follows to execute the program.

class Program
{


static void Main(string[] args)
{


CompanyGeneric BCompany = new CompanyGeneric();


foreach (int i in BCompany.BEmployeeList.Values)
{

Console.WriteLine(i);

}
Console.ReadKey();






}
}


I hope you understood the need of generics in development of an application.I know generics is a complex topic and it is very difficult to understand if you are especially a beginner.Try to run the above code and experience it yourself.Only then you will understand the exact usage of generics

If you have any questions
Mail me @ shaiksameer.cse@gmail.com.
I hope this has proven to be of some help to you.
Have Fun
Sameer Shaik

Free MCTS EXAM training tutorials on this website, soon will be launching video tutorials for all the same topics

No comments:

Post a Comment