Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# When should I use List and when should I use arraylist?

Tags:

c#

list

arraylist

As the title says when should I use List and when should I use ArrayList?

Thanks

like image 446
Scott Avatar asked Apr 07 '09 12:04

Scott


2 Answers

The main time to use ArrayList is in .NET 1.1

Other than that, List<T> all the way (for your local T)...

For those (rare) cases where you don't know the type up-front (and can't use generics), even List<object> is more helpful than ArrayList (IMO).

like image 85
Marc Gravell Avatar answered Sep 19 '22 03:09

Marc Gravell


You should always use List<TypeOfChoice> (introduced in .NET 2.0 with generics) since it is TypeSafe and faster than ArrayList (no un-necessary boxing/unboxing).

Only case I could think of where an ArrayList could be handy is if you need to interface with old stuff (.NET 1.1) or you need an array of objects of different type and you load up everything as object - but you could do the latter with List<Object> which is generally better.

like image 37
JohnIdol Avatar answered Sep 19 '22 03:09

JohnIdol