While building an application, we often encounter situations to store or retrieve objects and .NET gives plenty of storage options to do that. These options are categorized into generic and non-generic collection types. In this article, we are going to see two such storage types – ArrayList and List and compare them.

To download the source code for this article, you can visit our GitHub repository.

So, let’s start.

ArrayList and List Overview

ArrayList is a non-generic collection type that allows users to store objects of any data type and it is available under System.Collections namespace. We can learn more about ArrayList from this article: Working With Collections in .NET

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!

On the other way, a List is a generic collection type that allows users to store objects of data type as specified by the List at the time of declaration. It is a very strongly typed collection and is available under System.Collections.Generic namespace. We also have an article on List Collection in C#  explaining the concepts in depth.

Now it’s time to compare them.

Compare ArrayList and List

Let’s try to compare ArrayList and List under different categories.

Nature of Collection

By nature, ArrayList holds a heterogeneous collection of objects. This means that in a single instance of ArrayList, we can store objects of any data type. On the other hand, List is designed to hold just a homogeneous collection of objects. This means that in a single instance, we can store objects of only one data type. 

Let’s see an example of ArrayList storing different object types:

using System.Collections;

var arrayList = new ArrayList();
arrayList.Add(1); // integer
arrayList.Add(2);
arrayList.Add("3"); // string

And now let’s see an example on List that allows only one kind of object:

using System.Collections.Generic;

var list = new List<int>();
list.Add(1); // allows only integer values 
list.Add(2);
list.Add(3);

Error Prone

With ArrayList, we can always expect runtime errors while accessing collections as it stores heterogeneous objects. But List is a strongly typed collection that allows objects of the type defined as T in List<T>. Hence the app will throw a compile-time error if we ever try to store anything other than what was specified for T.

Now, using the same list instance, if we try to add a string, we will face a compile error:

list.Add("4"); // Gives compile error

Boxing/Unboxing Needs

When we use ArrayList, it often requires us to box or unbox the objects that we are accessing to avoid any errors. But with List, it’s never the case. 

Let’s see how:

int sum = 0;
foreach (var item in arrayList)
{
    sum += Convert.ToInt32(item);
}
 Console.WriteLine($"Sum is {sum}");

The output will be:

Sum is 6

As we can see with ArrayList we had to convert "3" to 3 using Convert.ToInt32() to make the computation work and avoid the runtime error.

But in the case of List, we don’t need to do that:

int sum = 0;
foreach (var item in list)
{
    sum += item;
}
Console.WriteLine($"Sum is {sum}");

And the output will still be the same:

Sum is 6

As we can see the List is type-safe and we didn’t have to do any type-casting at all.

Memory Management

Generally, List is more memory-efficient than ArrayList because it doesn’t have to store an object reference for every element in the collection. 

Performance Efficiency

From what we have seen so far, the List is more memory efficient than ArrayList. Also being type-safe makes it more performance efficient than ArrayList. The List has better API support and with the methods and properties it provides it makes it faster to access elements when compared to ArrayList.   

Usage Preferences

As we now know, ArrayList is flexible to store objects of any data type so we can use it if type safety is really not a concern. Also, if the application we’re developing is targeting a .NET framework version below .NET 2.0 we can continue to use ArrayList. However, if we’re working on a framework equal to or above .NET 2.0, we can always prefer List over ArrayList for many reasons that we have seen already in previous sections. 

Recommendation When Using ArrayList and List

By now, we can already see that we can use both collections to do similar functionalities. Yet List is a newer and better version of ArrayList.

As it is said – New is always better. The List should always be our first choice of collection for any new development. In fact, it is also suggested by Microsoft. We can use ArrayList if the application is still targeting a framework lesser than .NET 3.0 or if it’s an absolute necessity. 

Conclusion

In this article, we learned about ArrayList and List and when we use them. And it is evident that List wins over ArrayList in any comparison as it is efficient, better, and type-safe. So List it is!

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!