0
0
CsharpComparisonBeginner · 4 min read

List vs ArrayList in C#: Key Differences and When to Use Each

List<T> is a generic, type-safe collection introduced in .NET 2.0, while ArrayList is a non-generic collection that stores objects as object type. List<T> offers better performance and compile-time type checking compared to ArrayList.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of List<T> and ArrayList in C#.

FeatureListArrayList
Type SafetyYes, generic and type-checked at compile timeNo, stores elements as object, requires casting
PerformanceFaster due to no boxing/unboxingSlower due to boxing/unboxing of value types
Introduced in.NET Framework 2.0.NET Framework 1.1
NamespaceSystem.Collections.GenericSystem.Collections
UsagePreferred for new codeLegacy, mostly replaced by List
Supports LINQYesNo
⚖️

Key Differences

List<T> is a generic collection, meaning it stores elements of a specific type defined when you create the list. This provides type safety, so you get errors at compile time if you try to add the wrong type. ArrayList, on the other hand, stores elements as object, so you must cast elements back to their original type when retrieving them, which can cause runtime errors.

Because ArrayList stores everything as object, value types like int are boxed (wrapped) when added and unboxed when retrieved, which slows down performance. List<T> avoids this by storing the actual type directly, making it faster and more efficient.

Additionally, List<T> supports LINQ queries and modern C# features, making it more versatile and easier to use in current development. ArrayList is considered legacy and is rarely used in new projects.

⚖️

Code Comparison

Here is how you add and retrieve integers using List<int> in C#.

csharp
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int>();
        numbers.Add(10);
        numbers.Add(20);
        numbers.Add(30);

        foreach (int number in numbers)
        {
            Console.WriteLine(number);
        }
    }
}
Output
10 20 30
↔️

ArrayList Equivalent

Here is the equivalent code using ArrayList. Notice the need for casting when retrieving elements.

csharp
using System;
using System.Collections;

class Program
{
    static void Main()
    {
        ArrayList numbers = new ArrayList();
        numbers.Add(10);
        numbers.Add(20);
        numbers.Add(30);

        foreach (object obj in numbers)
        {
            int number = (int)obj; // Casting required
            Console.WriteLine(number);
        }
    }
}
Output
10 20 30
🎯

When to Use Which

Choose List<T> when you want type safety, better performance, and modern features like LINQ support. It is the recommended collection for most scenarios in current C# development.

Use ArrayList only if you are maintaining legacy code that already uses it or if you need to store mixed types without generics, though this is rare and not recommended.

Key Takeaways

List<T> is type-safe and faster than ArrayList.
ArrayList stores elements as object and requires casting.
List<T> supports LINQ and modern C# features.
Prefer List<T> for new development.
ArrayList is mainly for legacy compatibility.