07 Feb 2024




Intermediate

The main difference between ArrayList and List<T> in C# lies in their type safety, performance, and usage of generics:

  1. Type Safety:

    • ArrayList: ArrayList is a non-generic collection class available in C# that can store elements of any data type. Since ArrayList does not enforce type safety at compile time, it requires boxing and unboxing operations when storing and retrieving elements, which can lead to performance overhead and potential runtime errors if types are not handled correctly.
    • List<T>: List<T> is a generic collection class introduced in .NET Framework 2.0 that allows you to specify the type of elements it can store. It provides type safety at compile time, meaning you can only add elements of the specified type to the list. Because of its type safety, List<T> eliminates the need for boxing and unboxing operations, resulting in better performance and reduced risk of runtime errors.
  2. Performance:

    • ArrayList: Due to the lack of type safety, ArrayList relies on boxing and unboxing operations when working with value types, which can degrade performance, especially in scenarios involving frequent insertion, retrieval, and manipulation of elements.
    • List<T>: Since List<T> is type-safe and does not require boxing and unboxing operations, it offers better performance compared to ArrayList, especially when dealing with value types. It uses generics internally, which enables it to work efficiently with different types.
  3. Usage of Generics:

    • ArrayList: ArrayList is a non-generic collection class and does not use generics. As a result, it can store elements of any type, but it does not provide type safety at compile time.
    • List<T>: List<T> is a generic collection class that uses generics to enforce type safety at compile time. You specify the type of elements the list will store when you declare it, and the compiler ensures that only elements of that type can be added to the list.
FeatureArrayListList<T>
Type SafetyNot type-safe: can store any typeType-safe: enforces type at compile time
PerformanceMay involve boxing/unboxing operations, potentially impacting performance, especially with value typesBetter performance, as it avoids boxing/unboxing and provides direct access to elements
Usage of GenericsNon-generic: does not use genericsGeneric: uses generics to enforce type safety
Introduced.NET Framework 1.0.NET Framework 2.0
Recommended UsageLegacy code or when interfacing with APIs that require ArrayListPreferred choice for new development due to type safety and better performance
ExampleArrayList list = new ArrayList(); list.Add(42); list.Add("Hello");List<int> list = new List<int>(); list.Add(42);

In summary, while both ArrayList and List<T> are used to store collections of elements, List<T> is preferred in most cases due to its type safety, better performance, and usage of generics, which leads to more robust and efficient code. ArrayList is considered a legacy collection class and is typically used in scenarios where you need to work with code written before the introduction of generics in .NET Framework 2.0 or when interfacing with older APIs that require ArrayList.

c-sharp
arraylist
list
difference