ArrayList
List<>
C#
Data Structures
Programming

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) and unboxing occurs, which can impact performance.
  • Namespace: Exists within System.Collections.

Example

csharp
1ArrayList arrayList = new ArrayList();
2arrayList.Add(1);
3arrayList.Add("Hello World");
4arrayList.Add(true);
5
6// Access and iterate
7foreach (var item in arrayList)
8{
9    Console.WriteLine(item);
10}

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

csharp
1List<int> intList = new List<int>();
2intList.Add(1);
3intList.Add(2);
4intList.Add(3);
5
6// Access and iterate
7foreach (int number in intList)
8{
9    Console.WriteLine(number);
10}

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/AspectArrayListList<T>
Type SafetyNon-generic, no type safetyGeneric, strong type safety
Boxing/UnboxingRequired for value typesNo boxing/unboxing for value types
PerformanceGenerally slower due to boxingFaster due to lack of boxing
UsabilitySimple, but lacks flexibilityVersatile and safer
NamespaceSystem.CollectionsSystem.Collections.Generic
Better for MultithreadingNoNo, but can use List<T>.AsReadOnly
Usage ScenarioLegacy projectsModern 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 ArrayList is 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:

csharp
1ArrayList arrayList = new ArrayList() { 1, 2, 3 };
2int sum = 0;
3foreach (int num in arrayList)
4{
5    sum += num;
6}

Refactored List<T> Code:

csharp
1List<int> list = new List<int> { 1, 2, 3 };
2int sum = 0;
3foreach (int num in list)
4{
5    sum += num;
6}

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.


Course illustration
Course illustration

All Rights Reserved.