ArrayList vs List in C
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Overview
In C#, data collections are a foundational aspect that developers often work with. Among these collections, ArrayList and List<T> are two popular options. While they might appear similar at first glance, distinct differences exist between them that can significantly impact performance, type safety, and usability. This article provides a comprehensive comparison of ArrayList and List<T> in C#.
ArrayList
ArrayList is a part of the non-generic collections in C#. It was introduced in early versions of C# to allow working with a collection of objects whose size can dynamically grow or shrink. Here's a brief overview of its properties:
- Non-generic: Allows storing any type of object.
- Type Safety: Any data type can be added, leading to potential type safety issues.
- Boxing/Unboxing: When storing value types, conversion to
object(boxing) andunboxingoccurs, which can impact performance. - Namespace: Exists within
System.Collections.
Example
In the example above, different types of objects (int, string, boolean) are added to the ArrayList. This flexibility can lead to runtime errors if types are used incorrectly.
List<T>
Introduced with .NET 2.0, List<T> is a generic collection class that belongs to the System.Collections.Generic namespace. Key characteristics are:
- Generic: Strongly typed collection that ensures type safety.
- Performance: Eliminates the need for boxing/unboxing, offering better performance.
- Flexibility: Versatile, supporting any data types via generics
<T>. - Extensive API: Rich methods available for operations like searching, sorting, etc.
Example
Here, the List<int> can only store integers, which makes it safer and prevents runtime errors related to type casting.
Key Differences
To better understand the differences, refer to the table below:
| Feature/Aspect | ArrayList | List<T> |
| Type Safety | Non-generic, no type safety | Generic, strong type safety |
| Boxing/Unboxing | Required for value types | No boxing/unboxing for value types |
| Performance | Generally slower due to boxing | Faster due to lack of boxing |
| Usability | Simple, but lacks flexibility | Versatile and safer |
| Namespace | System.Collections | System.Collections.Generic |
| Better for Multithreading | No | No, but can use List<T>.AsReadOnly |
| Usage Scenario | Legacy projects | Modern development |
Subtopics
When to Use ArrayList vs List<T>?
- ArrayList: Rarely recommended in new development but might still be in use for legacy support. An
ArrayListis beneficial where object types are variable and you have legacy code constraints. - List benefits: Preferred in most cases due to type safety, performance advantages, and comprehensive API features. It's suitable for scenarios requiring consistency in data types and operations.
Migration from ArrayList to List<T>
Migrating an ArrayList to List<T> involves specifying the data type and adopting corresponding type-safe methods. Consider the following approaches:
Original ArrayList Code:
Refactored List<T> Code:
Role in Modern C#
With the advent of IEnumerable<T>, List<T>, and LINQ, developers have highly efficient and expressive options for handling collections. In modern C# programming, List<T> almost always supersedes ArrayList due to performance and safety advantages alone.
Conclusion
While ArrayList paved the way for dynamic collections in early C# programming, List<T> emerged as the superior choice, providing robust type safety and performance benefits. Even though ArrayList might still appear in legacy codebases, adopting List<T> is recommended for optimized, modern applications.

