Monday, October 20, 2008

List<T>

List<T>

List<T> is the new generic class introduced with .net 2.0 that is going to replace most of the Arraylist usage in our code. There are lot of reasons we would want to look at List<T> generic class ..

1. Arraylist collection allows any type (object). so if we put value types like int into arrraylist there is always a performance hit during adding (boxing) and retreival (unboxing ).

performance comparison- ArrayList’s vs. generic List
http://blogs.msdn.com/joshwil/archive/2004/04/13/112598.aspx

2. Strongly typed List<T> class allows type safety during compilation time like any other generic counter part and also reduces coding bugs.

3. Comparing List<T> with LinkedList<T>

LinkedList<T> is the doubly linked list class in .net framework. A List<T> is stored basically as a abig array in managed heap whereas LinkdList<T> can potentially have nodes all over the managed heap. So Inserting a new item into a List could be little more expensive when compared to LinkedList since it will involve shifting items depending on where you are inserting the new item.

List<T> outperform LinkedList<T> in the following area- Adding/removing nodes - ,indexed access/searching (List<T> uses indexer ,whereas LinkedList<T> you must navigate using the previous and next nodes ) .

-sree

No comments: