tags:

views:

138

answers:

4

Possible Duplicate:
What is cool about generics, why use them?

DUPLICATE:

http://stackoverflow.com/questions/77632/what-is-cool-about-generics-why-use-them


My basic question is same as title. But I want to know something more that can enlightened me with the world of C# generics.

Thanks! Aakash

+4  A: 

One of the to use a generic list instead of an ArrayList, is that ArrayList accepts objects on any type, and if you intend to store only objects of a given type, this can lead to runtime errors that can't happen with generics.

Example:

var numbers=new ArrayList();
numbers.Add(1);
numbers.Add("abcd"); //This will compile!

int theNumber=(int)numbers[1]; //This will cause an exception

When using a generic list, you ensure that only the desired type is stored in the list:

var numbers=new List<int>();
numbers.Add(1);
numbers.Add("abcd"); //This will NOT compile
Konamiman
You have been 10 seconds faster then I with exactly the same answer :-)
Stefan Steinegger
Not the same, I have a sample code ;-)
Konamiman
I decided to add a sample, but saw that you already wrote it ... :-)
Stefan Steinegger
A: 

A simple explanation.

When you have an list of integers, you just want to make sure that some part of the program adds a string into it. Using generics, you tell the compiler that you decide to only have integers (or whatever type) in the list and it makes sure that you don't break your own law.

You can say, it is another language (and runtime) feature to express your intention in code. This allows the tools to support you.

Stefan Steinegger
A: 

Further to the answers from Konamiman and Stefan, using generic collections when dealing with value-types will also avoid the cost of boxing/unboxing.

Every time you add a value-type to a non-generic collection it will need to be boxed, and it will then need to be unboxed when you remove it from the collection (assuming that you want to do anything type-specific with it).

ArrayList myList = new ArrayList();
myList.Add(42);             // box
myList.Add(DateTime.Now)    // box
myList.Add(3.14)            // box

int x = (int)myList[0];              // unbox
DateTime y = (DateTime)myList[1];    // unbox
double z = (double)myList[2];        // unbox
LukeH
A: 

As previous posters have said, there is a compile time safety aspect to using generic collections; they can catch some stupid mistakes at compile time (which is better than at runtime).

There are some other reasons to prefer them too, however:

  1. There is a small but occasionally quite relevant performance advantage. If you're dealing with a lot of collection operations in inner loops, the unnecessary type-checks the runtime must perform do add up. If you're computing the sum of integers in an arraylist, expect slower code than the equivalent approach using a List<int> store.
  2. Subtly, but in my mind very relevantly, generic collections aren't only analyzable compile-time, but `coding'-time. This enables intellisense, which is a great feature to have.
  3. Even if you use these collection with values of uncertain type, you'll get the more attractive fail-fast error handling usually.

For instance:

object x = "5";

var intlist = new List<int>();
intlist.Add((int)x); //fails early; compiler requires this cast
//...perhaps much later...
foreach(int num in intlist)...

var arraylist = new ArrayList();
arraylist.Add(x); //this is OK though a cast would be advisable
//...perhaps much later...
foreach(int num in arraylist) ... //fails late

Late failure is harder to debug than early failure since the point when failure is detected is further removed from the original coding error.

Eamon Nerbonne