What is the difference between ArrayList
and List<>
in C#?
Is it only that List<>
has a type while ArrayLis
ArrayList
is the collections of different types data whereas List<>
is the collection of similar type of its own depedencties.
Simple Answer is,
ArrayList arrayList = new ArrayList();
List<int> list = new List<int>();
arrayList.Add(1);
arrayList.Add("String");
arrayList.Add(new object());
list.Add(1);
list.Add("String"); // Compile-time Error
list.Add(new object()); // Compile-time Error
Please read the Microsoft official document: https://blogs.msdn.microsoft.com/kcwalina/2005/09/23/system-collections-vs-system-collection-generic-and-system-collections-objectmodel/
Note: You should know Generics before understanding the difference: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/
I think, the differences between ArrayList
and List<T>
are:
List<T>
, where T is value-type is faster than ArrayList
. This is
because List<T>
avoids boxing/unboxing (where T is value-type).ArrayList
used just for backward
compatibility. (is not a real difference, but i think it is
important note).ArrayList
then List<T>
ArrayList
has IsSynchronized
property. So, It is easy
to create and use syncronised ArrayList
. I didin't found IsSynchronized
property for List<T>
. Also Keep in mind this type of synchronization is relatively inefficient, msdn):
var arraylist = new ArrayList();
var arrayListSyncronized = ArrayList.Synchronized(arraylist
Console.WriteLine($"syncronized {arraylist.IsSynchronized}");
Console.WriteLine($"syncronized {arrayListSyncronized.IsSynchronized}");
var list = new List<object>();
var listSyncronized = ArrayList.Synchronized(list);
Console.WriteLine($"syncronized {list.IsSynchronized}");//error, no such prop
Console.WriteLine($"syncronized {list.IsSynchronized}");//error, no such prop
ArrayList
has ArrayList.SyncRoot
property which can be used for syncronisation (msdn). List<T>
hasn't SyncRoot
property, so in
the following construction you need to use some object if you use List<T>
:
ArrayList myCollection = new ArrayList();
lock(myCollection.SyncRoot) // ofcourse you can use another object for this goal
{
foreach (object item in myCollection)
{
// ...
}
}
As mentioned in .NET Framework documentation
We don't recommend that you use the
ArrayList
class for new development. Instead, we recommend that you use the genericList<T>
class. TheArrayList
class is designed to hold heterogeneous collections of objects. However, it does not always offer the best performance. Instead, we recommend the following:
- For a heterogeneous collection of objects, use the
List<Object>
(in C#) orList(Of Object)
(in Visual Basic) type.- For a homogeneous collection of objects, use the
List<T>
class.
See also Non-generic collections shouldn't be used
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.
ArrayList
are not type safe whereas List<T>
are type safe. Simple :).