ArrayList is a list that grows as you add items. It was the go-to resizable collection in .NET 1.0, and it has been superseded since .NET 2.0 introduced generics and List<T>. Use List<T> in new code. The ArrayList section further down is for people maintaining older projects.
The same example with List<T>
List<string> items = ["Element 1", "Element 2", "Element 3"];
Console.WriteLine($"Count: {items.Count}");
Console.WriteLine($"Contains Element 2: {items.Contains("Element 2")}");
items.Remove("Element 2");
Console.WriteLine($"Count: {items.Count}");
Output:
Count: 3
Contains Element 2: True
Count: 2
Remove deletes the first matching item and returns true if it found one. It does not throw when the item is missing.
Other operations you will use
List<int> numbers = [5, 3, 8];
numbers.Add(1); // [5, 3, 8, 1]
numbers.Insert(0, 10); // [10, 5, 3, 8, 1]
numbers.AddRange([7, 7]); // [10, 5, 3, 8, 1, 7, 7]
numbers.RemoveAt(1); // [10, 3, 8, 1, 7, 7]
numbers.RemoveAll(n => n == 7); // [10, 3, 8, 1]
numbers.Sort(); // [1, 3, 8, 10]
int index = numbers.IndexOf(8); // 2, or -1 if not found
Console.WriteLine(string.Join(", ", numbers));
// 1, 3, 8, 10
A few things that catch people out:
- Removing while looping. Calling
Removeinside aforeachover the same list throwsInvalidOperationException(“Collection was modified”). UseRemoveAllwith a condition, or loop backwards with aforloop. ContainsandIndexOfare linear. They check every item. If you test membership often on a large list, put the items in aHashSet<T>as well.Insert(0, ...)shifts everything. Inserting at the front of a large list copies every element. AQueue<T>orLinkedList<T>may fit better.- Capacity. A list doubles its internal array when it runs out of room. If you know the final size,
new List<int>(10_000)avoids the repeated copying.
Why ArrayList fell out of use
ArrayList stores every item as object. That causes two problems.
The first is type safety. Anything can go into an ArrayList, and the mistake only shows up when you read the item back:
using System.Collections;
var list = new ArrayList();
list.Add(42);
list.Add("forty-two"); // compiles fine
int total = 0;
foreach (object item in list)
{
total += (int)item; // InvalidCastException on the second item
}
With List<int>, the second Add is a compile error.
The second is performance. Adding an int to an ArrayList boxes it: the runtime allocates a small object on the heap to hold the number, and reading it back unboxes it again. On a list of a million integers that means a million extra allocations, while List<int> stores the numbers directly in its internal array.
ArrayList is not marked obsolete and still exists in current .NET, so legacy code keeps compiling. Microsoft’s own documentation recommends against using it for new development.
Maintaining code that uses ArrayList
The original version of this example looked like this, and it still runs:
using System.Collections;
ArrayList arrayList = new ArrayList();
arrayList.Add("Element 1");
arrayList.Add("Element 2");
arrayList.Add("Element 3");
Console.WriteLine("ArrayList Count: " + arrayList.Count);
Console.WriteLine("Contains Element 2: " + arrayList.Contains("Element 2"));
arrayList.Remove("Element 2");
Console.WriteLine("ArrayList Count: " + arrayList.Count);
When an older API hands you an ArrayList, convert it at the boundary and work with a typed list from there:
List<string> typed = arrayList.Cast<string>().ToList();
Cast<T> throws if an element has a different type. If the list may contain mixed items and you only want some of them, use OfType<T>(), which skips the rest. If you genuinely need a list of mixed types, List<object> says so honestly and still gives you the full List<T> API.
1 thought on “C# ArrayList vs Generic List: Examples and When to Use Each”