Open In App

ArrayList Class in C#

Last Updated : 18 Oct, 2025
Comments
Improve
Suggest changes
12 Likes
Like
Report

ArrayList class in C# is a part of the System.Collections namespace that represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. It also allows dynamic memory allocation, adding, searching and sorting items in the list.

  • Elements can be added or removed from the Array List collection at any point in time.
  • The ArrayList is not guaranteed to be sorted.
  • The capacity of an ArrayList is the number of elements the ArrayList can hold.
  • Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based.
  • It also allows duplicate elements.
  • Using multidimensional arrays as elements in an ArrayList collection is not supported.

Example: Declaration and Initialization

C#
using System;
using System.Collections;

class Program
{
    static void Main()
    {
        // Declaring an ArrayList
        ArrayList al = new ArrayList();

        // Adding elements
        al.Add(1);
        al.Add(2);
        al.Add(3);

        // Iterating and printing elements
        foreach(var item in al)
            Console.WriteLine(item);
    }
}

Output
1
2
3

Constructors

  • ArrayList(): Initializes a new instance of the ArrayList class that is empty and has the default initial capacity.
  • ArrayList(int capacity): Creates an empty ArrayList with the specified initial capacity.
  • ArrayList(ICollection collection): Creates an ArrayList containing elements copied from the specified collection.

Example: Count and Capacity

C#
ArrayList aL = new ArrayList();
aL.Add("Hello");
aL.Add("World");

Console.WriteLine("Count: " + aL.Count);
Console.WriteLine("Capacity: " + aL.Capacity);

Output:

Count: 2

Capacity: 4

Note: The default capacity is 4. ArrayList automatically resizes when the number of elements exceeds the current capacity, usually doubling the size.

Properties

The ArrayList class provides several properties to access its state.

PropertiesDescription
CapacityGets or sets the number of elements that the ArrayList can contain.
CountGets the number of elements actually contained in the ArrayList.
IsFixedSizeGets a value indicating whether the ArrayList has a fixed size.
IsReadOnlyGets a value indicating whether the ArrayList is read-only.
IsSynchronizedGets a value indicating whether access to the ArrayList is synchronized (thread safe).
Item[Int32]Gets or sets the element at the specified index.
SyncRootGets an object that can be used to synchronize access to the ArrayList.

Example: IsFixedSize and IsReadOnly

C#
ArrayList al = new ArrayList();
al.AddRange(new[] { "A", "B", "C", "D" });

Console.WriteLine(al.IsFixedSize);  // False
Console.WriteLine(al.IsReadOnly);   // False

Methods

  • Add(Object): Adds an element to the end of the ArrayList
  • AddRange(ICollection): Adds a collection of elements to the end
  • Remove(Object): Removes the first occurrence of a specific element
  • RemoveAt(Int32): Removes an element at a specific index
  • RemoveRange(Int32, Int32): Removes a range of elements
  • Insert(Int32, Object): Inserts an element at a specific index
  • InsertRange(Int32, ICollection): Inserts a collection of elements at a specific index
  • Contains(Object): Checks if an element exists in the ArrayList
  • IndexOf(Object): Returns the index of the first occurrence of an element
  • LastIndexOf(Object): Returns the index of the last occurrence of an element
  • Clear(): Removes all elements
  • Sort() / Sort(IComparer): Sorts elements in ascending order or using a custom comparer
  • Reverse() / Reverse(Int32, Int32): Reverses the order of elements
  • BinarySearch(Object) / BinarySearch(Object, IComparer): Searches a sorted ArrayList for an element
  • ToArray() / ToArray(Type): Copies elements to a new array
  • Clone(): Creates a shallow copy
  • TrimToSize(): Adjusts the capacity to match the number of elements
  • ReadOnly(ArrayList): Returns a read-only wrapper
  • Synchronized(ArrayList): Returns a thread-safe wrapper

Example: Checking for an Element

C#
using System;
using System.Collections;
using System.Collections.Generic;

class Geeks {
    public static void Main(){

        // Creating an ArrayList
        ArrayList al = new ArrayList();

        // Adding elements to ArrayList
        al.Add("A");
        al.Add("B");
        al.Add("C");
        al.Add("D");
        al.Add("E");

        if (al.Contains("E"))
            Console.WriteLine("Yes, exists at index "
                              + al.IndexOf("E"));
        else
            Console.WriteLine("No, doesn't exists");
    }
}

Output
Yes, exists at index 4

Example: Removing a Range of Elements

C#
using System;
using System.Collections;
using System.Collections.Generic;

class Geeks {
    public static void Main(){

        // Creating an ArrayList
        ArrayList al = new ArrayList(10);

        // Adding elements to ArrayList
        al.Add(2);
        al.Add(4);
        al.Add(6);
        al.Add(8);

        // Displaying the elements in ArrayList
        Console.WriteLine("The initial ArrayList: ");

        foreach(int i in al) { 
          Console.WriteLine(i); 
          }

        // removing 4 elements starting from index 0
        al.RemoveRange(0, 2);

        // Displaying the modified ArrayList
        Console.WriteLine("The ArrayList after Removing elements: ");

        // Displaying the elements in ArrayList
        foreach(int i in al) { 
          Console.WriteLine(i); 
        }
    }
}

Output
The initial ArrayList: 
2
4
6
8
The ArrayList after Removing elements: 
6
8

Explore