Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList vs List<> in C#

What is the difference between ArrayList and List<> in C#?

Is it only that List<> has a type while ArrayList doesn't?

like image 273
scatman Avatar asked Feb 22 '10 08:02

scatman


People also ask

Should I use ArrayList or List?

There is not much difference in this. The only difference is, you are creating a reference of the parent interface in the first one and a reference of the class which implements the List (i.e) the ArrayList class in the second.

Is C# List an ArrayList?

C# ArrayList is a non-generic collection. The ArrayList class represents an array list and it can contain elements of any data types. The ArrayList class is defined in the System. Collections namespace.


1 Answers

Yes, pretty much. List<T> is a generic class. It supports storing values of a specific type without casting to or from object (which would have incurred boxing/unboxing overhead when T is a value type in the ArrayList case). ArrayList simply stores object references. As a generic collection, List<T> implements the generic IEnumerable<T> interface and can be used easily in LINQ (without requiring any Cast or OfType call).

ArrayList belongs to the days that C# didn't have generics. It's deprecated in favor of List<T>. You shouldn't use ArrayList in new code that targets .NET >= 2.0 unless you have to interface with an old API that uses it.

like image 169
mmx Avatar answered Oct 02 '22 14:10

mmx