C-SHARP - 4.4 ArrayList
This video introduces the ArrayList in C#. Until now arrays and generic lists only accepted items of a single declared type. An ArrayList is similar, but it can store values of different types in the same collection. To use it we add using System.Collections; at the top of the file because that is where the class lives.
We declare the list with ArrayList liste = new ArrayList();, then add elements either with liste.Add(value) or with the initializer syntax between braces. We can mix an int, a bool, a string and other types freely — no type is specified at construction. Each cell is exposed as an object, which is the root C# type that can hold anything.
using System.Collections;
ArrayList liste = new ArrayList { 18, true, "Bonjour" };
int age = (int)liste[0] + 1; // explicit cast required
Console.WriteLine(age);
for (int i = 0; i < liste.Count; i++)
{
Console.WriteLine(liste[i]);
}
Why ArrayList is rarely used today
- Every element is typed as
object: you must cast to operate on the real type. - If you cast to the wrong type, the program compiles but throws at runtime.
- Mixing types is a frequent source of bugs that are hard to track.
- Prefer strongly typed collections: arrays,
List<T>or dictionaries.
If you try int age = liste[0] + 1; the compiler refuses because the operator + is not defined on object. You have to write (int)liste[0] to force a cast. The same logic applies to any operation. Mixing types is a real source of confusion and runtime exceptions, which is why most C# developers stick to strongly typed collections. We will see dictionaries in the next lesson, which combine flexibility and type safety.
Summary
This lesson introduces ArrayList in C#, a collection class that allows storing multiple elements of different data types in a single container. Unlike strongly-typed arrays and generic lists that restrict elements to a single type, ArrayList provides flexibility at the cost of requiring explicit type casting when retrieving values. The lesson covers declaration, initialization, iteration, and demonstrates why ArrayList is rarely recommended in modern C# development due to type safety concerns and potential runtime exceptions.
Key points
- ArrayList is declared using `new ArrayList()` and requires the `using System.Collections;` namespace
- Unlike arrays and lists, ArrayList can store mixed types (integers, strings, booleans, etc.) without prior type specification
- Accessing ArrayList elements returns `object` type, requiring explicit casting with syntax like `(int)liste[0]` to convert to specific types
- ArrayList is considered a poor programming practice because mixing types creates confusion and makes code prone to InvalidCastException errors at runtime
- Elements are added using the `Add()` method or through initialization syntax with curly braces
- Iteration over ArrayList uses standard for loops with the `.Count` property to determine collection size
FAQ
When should I use ArrayList instead of List<T> in C#?
ArrayList should be avoided in modern C# development. Use generic List<T> instead, as it provides type safety and better performance. ArrayList remains in the framework primarily for legacy code compatibility.
Why do I get an InvalidCastException when accessing ArrayList elements?
Because ArrayList stores all elements as `object` type, accessing an element and casting it to the wrong type will throw an exception. You must ensure you cast to the correct data type that was originally stored—for example, casting an integer value to int, or a string value to string.
Do I need to specify a type when creating an ArrayList?
No. Unlike arrays and generic lists, ArrayList accepts any type without declaration. This flexibility is its main feature but also its primary drawback, as it requires manual type casting and creates opportunities for runtime errors.