English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# ArrayList (dynamic array)

In C#, ArrayList is a non-generic object collection whose size increases dynamically. It is similar to Array, but its size increases dynamically.

An ArrayList can be used to add unknown data, possibly types and data sizes you are not aware of.

Create ArrayList

The ArrayList class included in the System.Collections namespace. The ArrayList is created as an object using the new keyword.

using System.Collections;
ArrayList arlist = new ArrayList(); 
// Or
var arlist = new ArrayList(); // Recommended

Add elements to ArrayList

Add elements to ArrayList using the Add() method or object initializer syntax.

ArrayList can contain multiple nulls and duplicate values.

// Add elements using the ArrayList.Add() method
var arlist1 = new ArrayList();
arlist1.Add(1);
arlist1.Add("Bill");
arlist1.Add(" ");
arlist1.Add(true);
arlist1.Add(4.5);
arlist1.Add(null);
// Add elements using object initialization syntax
var arlist2 = new ArrayList()
                {
                    2, "Steve", "", true 4.5, null
                };

Use the AddRange (ICollection c) method to add a complete array, HashTable, SortedList, ArrayList, BitArray, Queue, and Stack to an ArrayList.

var arlist1 = new ArrayList();
var arlist2 = new ArrayList()
                    {
                        1, "Bill", " ", true, 4.5, null
                    };
int[] arr = { 100, 200, 300, 400 };
Queue myQ = new Queue();
myQ.Enqueue("Hello");
myQ.Enqueue("World!");
arlist1.AddRange(arlist2); //Add an ArrayList to arraylist 
arlist1.AddRange(arr); //Add an array to arraylist 
arlist1.AddRange(myQ); //Add a queue to arraylist

Access ArrayList

This ArrayList class implements the IList interface. Therefore, elements can be accessed using indexers as if they were an array. The index starts at zero and increases for each subsequent element 1.

Need to convert to the appropriate type explicitly, or use a var variable.

var arlist = new ArrayList()
                {
                    1,
                    "Bill",
                    300,
                    4.5f
                };
//Use indexers to access individual items
int firstElement = (int) arlist[0]; //Returns1
string secondElement = (string) arlist[1]; //Returns "Bill"
//int secondElement = (int) arlist[1]; //Error: Cannot cast string to int
//Use the var keyword without explicit conversion
var firstElement = arlist[0]; //Returns1
var secondElement = arlist[1]; //Returns "Bill"
//var fifthElement = arlist[5]; //Error: Index out of range
//Update element
arlist[0] = "Steve"; 
arlist[1] = 100;
//arlist[5] = 500; //Error: Index out of range

Traverse ArrayList

The ArrayList class implements the ICollection interface, which supports iteration of collection types. Therefore, ArrayList is iterated using foreach and for loops. The Count property of ArrayList returns the total number of elements in the ArrayList.

ArrayList arlist = new ArrayList()
                        {
                            1,
                            "Bill",
                            300,
                            4.5F
                        };
foreach(var item in arlist)
    Console.Write(item + ", "); //Output:1,Bill,300,4.5, 
            
for(int i = 0; i < arlist.Count;++)
    Console.Write(arlist[i] + ", "); //Output:1,Bill,300,4.5,

Insert elements into ArrayList

Use the Insert() method to insert the specified element at the specified index in the ArrayList.

Syntax:

void Insert(int index, Object value)
ArrayList arlist = new ArrayList()
                {
                    1,
                    "Bill",
                    300,
                    4.5f
                };
arlist.Insert(1, "Second Item");
foreach(var val in arlist)
    Console.WriteLine(val);

Use the InsertRange() method to insert a collection at the specified index in the ArrayList.

Syntax: 

void InsertRange(int index, ICollection c)
ArrayList arlist1 = new ArrayList()
                {
                    100, 200, 600
                };
ArrayList arlist2 = new ArrayList()
                {
                    300, 400, 500
                };
arlist1.InsertRange(2, arlist2);
foreach(var item in arlist1)
    Console.Write(item + ", "); //Output:100、200、300、400、500、600,

Delete elements from ArrayList

Remove elements from ArrayList using Remove(), RemoveAt(), or RemoveRange methods.

ArrayList arList = new ArrayList()
                {
                    1,
                    null,
                    "Bill",
                    300,
                    " ",
                    4.5f,
                    300,
                };
arList.Remove(null); //Delete the first occurrence of null
arList.RemoveAt(4); //Delete index4element at
arList.RemoveRange(0, 2);//Delete two elements starting from the first item (index 0)

Check the elements in the ArrayList

Determine whether the specified element exists in the ArrayList using the Contains() method. If it exists, return true; otherwise, return false.

ArrayList arList = new ArrayList()
                {
                    1,
                    "Bill",
                    300,
                    4.5f,
                    300
                };
Console.WriteLine(arList.Contains(300)); // 
Console.WriteLine(arList.Contains("Bill")); // 
Console.WriteLine(arList.Contains(10)); // false
Console.WriteLine(arList.Contains("Steve")); // false
ArrayList due to performance issues, It is not recommended to use this class. Instead, use List<object> to store heterogeneous objects. To store data of the same data type, please use generic List<T>.

ArrayList Class

The following figure illustrates the ArrayList class.

C# ArrayList

ArrayList Properties

Properties
Description
Capacity

Gets or sets the number of elements the ArrayList can contain.

Count

Gets the number of elements actually contained in the ArrayList.

IsFixedSize

Gets a value that indicates whether the ArrayList has a fixed size.

IsReadOnly

Gets a value that indicates whether the ArrayList is read-only.

Item

Gets or sets the element at the specified index.

ArrayList Methods

MethodDescription
Add()/AddRange()

The Add () method adds a single element to the end of the ArrayList.

The AddRange () method adds all elements of the specified collection to the ArrayList.

Insert()/InsertRange()

The Insert () method inserts a single element at the specified index in the ArrayList.

The InsertRange () method inserts all elements of the specified collection starting from the specified index in the ArrayList.

Remove()/RemoveRange()

The Remove () method deletes the specified element from the ArrayList.

The RemoveRange () method deletes the specified range of elements from the ArrayList.

RemoveAt()

Removes the element at the specified index from the ArrayList.

Sort()

Sorts all elements in the ArrayList.

Reverse()

Reverses the order of elements in the entire ArrayList.

Contains

Checks if the specified element exists in ArrayList. If it exists, it returns true; otherwise, it returns false.

Clear

Removes all elements from ArrayList.

CopyTo

Copies all elements or a range of elements to a compile-time array.

GetRange

Returns the number of elements from ArrayList at the specified index.

IndexOf

Searches for the specified element and returns the zero-based index of the element if found. If the element is not found, it returns-1.

ToArray

Returns an array from ArrayList.