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#.
| Feature | List | ArrayList |
|---|---|---|
| Type Safety | Yes, generic and type-checked at compile time | No, stores elements as object, requires casting |
| Performance | Faster due to no boxing/unboxing | Slower due to boxing/unboxing of value types |
| Introduced in | .NET Framework 2.0 | .NET Framework 1.1 |
| Namespace | System.Collections.Generic | System.Collections |
| Usage | Preferred for new code | Legacy, mostly replaced by List |
| Supports LINQ | Yes | No |
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#.
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); } } }
ArrayList Equivalent
Here is the equivalent code using ArrayList. Notice the need for casting when retrieving elements.
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); } } }
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.List<T> for new development.ArrayList is mainly for legacy compatibility.