I recently answered the question How to add elements to an array in C#? and while I was proposing the alternative solution which was to use the class List
, I reminded myself that there is also a class called ArrayList
.
When comparing the documentation of ArrayList and List , in some parts of these articles it says:
"The ArrayList class is designed to hold heterogeneous collections of objects. However, it does not always offer the best performance."
And also
"The List class performs best in most cases and is type-safe."
Then the article List
tries to explain why it performs List
better, but this is the part I don't understand. It literally says:
"For a List, if a value type is used for type T, the compiler generates an implementation of the List class specifically for that value type. That means a list element of a List object does not have to be boxed before the element can be used."
"For a List, if a type is used for the type in the list, the compiler generates an implementation of the List class specifically for that type value. That means that a list element of the List object does not have to be encased before it can to be used."
*Free translation by me
What is really the difference between ArrayList and List and why does it say List has better performance?
PS: +10 brownie points to anyone who tells me what I should understand by "boxed" in that paragraph, that the truth is I couldn't translate it very well. Edit: Flxtr has taken the brownie points.
ArrayList
is a class that was created since version 1 of C# and the .NET Framework before Generics was added , while itList<T>
is the class that has replaced it since C# 2, the .NET Framework 2.0, and VB.NET 2008 (8.0) onwards .The main difference is that an instance of
ArrayList
allows multiple types to be added to the collection.Example:
C#
VB
While it
List<T>
needs the type of the elements of the list to be defined when it is instantiatedC#
VB
And about the performance the difference is in the terms boxing and unboxing
Boxing refers to the fact of wrapping a value of type Reference type (such as
int
,char
,float
,double
, ... among others) inside a reference if it is to be assigned to a variable or parameter of typeobject
. Unboxing refers to the reverse process, that is, to obtain the value of the object in a variable of type reference type .The problem is that this boxing and unboxing process occurs in the case of
ArrayList
when a value is added or obtained.Examples:
C#
VB
This process takes execution time so it is slower than the generic case.
In the generic case with
List<T>
this process it is non-existent since the compiler generates the code for the exact data types, (in this caseint
) and does not require any type of conversion.C#
VB
I recommend that you always use
List<T>
the truth, I do not know of a case where it is convenient to useArrayList
it, since although it has not been marked as obsolete, in practice it is no longer used.Basically, ArrayList needs to box and unbox each time an array element is saved/accessed. This is so because ArrayList stores references to objects of type object, so it needs to perform the cast task continuously.
On the other hand, List doesn't need to perform that task, since the compiler implements a concrete (specialized?) list for the type T to work with. For this reason, it is also more secure (although it does not ensure eye ordering either).
The difference could be understood like this:
By declaring List you assign it a type (String, integer, Boolean) and as you add elements it is a process of assigning values of a specific type to a memory sector.
ArrayList instead converts the information it receives into a completely new object before allocating space in memory (boxing) and so on for each item in the arrayList. When reading it you must not only get it from RAM space, but also "create" it and all of its properties. which consumes a lot of resources
The List(Of T) class -T refers to Type (a fixed type)- is very powerful. One of its greatest qualities is to implement the IEnumerable(Of T) interface, which allows access to many methods. An object of this class can be easily transformed into an array using the ToArray method. We can read the MSDN documentation about this class at: List(Of T) on MSDN
An important advantage of List(Of T) is that objects of this class can be part of LINQ language queries, allowing us to transform lists of objects of one type into lists of objects of another type or into a statistical object as the first element of a query. list, last, max, min... LINQ