System.Collections.ArrayList Class

Namespace: System.Collections

Represents a non-generic collection of objects that can be individually accessed by an integer index. The size of the ArrayList is automatically adjusted as elements are added or removed.

Note: The ArrayList class is a legacy collection class that is not type-safe. For new development, it is recommended to use the generic System.Collections.Generic.List<T> class instead.

Syntax

public class ArrayList : IList, ICollection, IEnumerable, ICloneable

Remarks

The ArrayList provides a dynamic array that can grow or shrink as needed to accommodate the number of elements and the range of index values.

All elements in an ArrayList are stored as objects.

When you add an element to an ArrayList, it is cast to an Object. Similarly, when you retrieve an element, it must be cast back to its original type. If you are adding value types (such as structs), you must be aware of boxing and unboxing operations, which can affect performance.

The ArrayList is not guaranteed to be thread-safe. You can use ArrayList.Synchronized to get a thread-safe version of the ArrayList, but this is not recommended as it can be a performance bottleneck. For thread-safe collections, consider using types from the System.Collections.Concurrent namespace.

Constructors

Name Description
ArrayList() Initializes a new instance of the ArrayList class that is empty, has the initial capacity of zero, and uses the default load factor.
ArrayList(int capacity) Initializes a new instance of the ArrayList class that is empty and has the specified initial capacity or is empty and has the default initial capacity if the specified capacity is less than or equal to zero.
ArrayList(ICollection c) Initializes a new instance of the ArrayList class that contains elements copied from the specified collection and has enough capacity to accommodate the number of elements copied.

Methods

The ArrayList class provides a rich set of methods for manipulating the collection, including:

  • Add(): Adds an element to the end of the ArrayList.
  • Insert(): Inserts an element at a specified index.
  • Remove(): Removes the first occurrence of a specific element.
  • RemoveAt(): Removes the element at a specified index.
  • Clear(): Removes all elements from the ArrayList.
  • Contains(): Determines whether an element is in the ArrayList.
  • IndexOf(): Returns the zero-based index of the first occurrence of a value in the entire ArrayList.
  • Sort(): Sorts the elements in the entire ArrayList.
  • Reverse(): Reverses the order of the elements in the entire ArrayList.
  • TrimToSize(): Sets the capacity of the ArrayList to the actual number of elements in the ArrayList.

Properties

  • Capacity: Gets or sets the number of elements that the ArrayList can contain.
  • Count: Gets the number of elements actually contained in the ArrayList.
  • IsFixedSize: Gets a value indicating whether the ArrayList has a fixed size.
  • IsReadOnly: Gets a value indicating whether access to the ArrayList is synchronized (thread-safe).

Example

using System.Collections;
using System;

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

        // Add elements
        list.Add("Hello");
        list.Add(123);
        list.Add(true);

        // Access elements (requires casting)
        string greeting = (string)list[0];
        int number = (int)list[1];
        bool flag = (bool)list[2];

        Console.WriteLine($"Greeting: {greeting}, Number: {number}, Flag: {flag}");

        // Remove an element
        list.Remove(123);

        // Iterate through the ArrayList
        Console.WriteLine("Elements after removal:");
        foreach (object item in list)
        {
            Console.WriteLine(item);
        }
    }
}