Introduction

The ArrayList class represents a strongly typed list of objects that can be accessed by index. Unlike an array, an ArrayList dynamically resizes itself as elements are added or removed.

The ArrayList class is part of the System.Collections namespace. While it provides a convenient way to manage collections of objects, it's generally recommended to use the generic collection types from the System.Collections.Generic namespace (like List<T>) for type safety and better performance in modern .NET development.

Declaration


public class ArrayList : IList, ICollection, IEnumerable, ICloneable
                

Syntax


using System.Collections;

public class ArrayList : IList, ICollection, IEnumerable, ICloneable
                

Inheritance Hierarchy

Object > ArrayList

Thread Safety

Instances of ArrayList are not thread-safe.

Note: If you require thread safety, consider using the synchronized wrapper returned by the Synchronized(ICollection) method or using the generic List<T> class from the System.Collections.Generic namespace.

Remarks

The ArrayList class provides a dynamic array that can grow or shrink as needed. It can store any type of object, including null. However, storing value types will involve boxing and unboxing operations, which can impact performance. For collections of value types, consider using a generic collection like List<T>.

Key features of ArrayList include:

  • Dynamic Sizing: Automatically resizes when elements are added beyond its current capacity.
  • Object Storage: Can store objects of any type.
  • Indexed Access: Elements can be accessed using an integer index (0-based).
  • Rich API: Provides methods for adding, removing, searching, sorting, and manipulating elements.

When to use ArrayList:

  • Working with legacy code that uses ArrayList.
  • When you need a collection that can hold mixed data types and the strict type safety of generic collections is not a primary concern.

When to prefer List<T>:

  • For new development, as it offers type safety, better performance, and a more modern API.
  • When working exclusively with a specific data type.

Example

The following example demonstrates how to create an ArrayList, add elements, and access them.


using System;
using System.Collections;

public class Example
{
    public static void Main(string[] args)
    {
        // Create an ArrayList
        ArrayList myList = new ArrayList();

        // Add elements of different types
        myList.Add("Hello");
        myList.Add(123);
        myList.Add(true);
        myList.Add(3.14);

        Console.WriteLine("Elements in the ArrayList:");
        // Iterate and print elements
        foreach (object obj in myList)
        {
            Console.WriteLine(obj);
        }

        // Access an element by index
        Console.WriteLine($"Element at index 1: {myList[1]}");

        // Remove an element
        myList.Remove("Hello");

        Console.WriteLine("\nArrayList after removing 'Hello':");
        foreach (object obj in myList)
        {
            Console.WriteLine(obj);
        }
    }
}
                

Performance

ArrayList performance can be affected by several factors:

  • Boxing and Unboxing: When storing value types (like int, bool, struct) in an ArrayList, they are boxed into objects. Retrieving them requires unboxing. These operations incur a performance overhead.
  • Capacity Management: When an ArrayList needs to grow, it allocates a new, larger internal array and copies all existing elements to the new array. This can be computationally expensive if it happens frequently. You can optimize this by specifying an initial capacity if you have an estimate of the number of elements.
  • Type Checking: Since ArrayList stores objects, the runtime may need to perform type checks, which can add to processing time.

Exceptions

The ArrayList class can throw various exceptions, including:

  • ArgumentOutOfRangeException: If an index is out of bounds.
  • InvalidOperationException: If an operation is performed on a disposed object or in an invalid state.
  • NotSupportedException: If a method is called that is not supported by the collection.

Requirements

  • Namespace: System.Collections
  • Assembly: mscorlib.dll

See Also