C# ArrayList Class

The System.Collection.ArrayList class is similar to arrays, but can store elements of any data type. It is collection that can be declared with or without specifies its size. The size of the ArrayList can grow dynamically as the number of elements increases. ArrayList can be accessed using foreach or for loop or indexer.

ArrayList list = new ArrayList(); //declaration without size (dynamic)
		Or
ArrayList list = new ArrayList(20); //declaration with size
ArrayList properties
S.No Property Description
CapacityGets or sets the number of elements the ArrayList can contain.
CountGets the exact number of elements in the ArrayList.
ArrayList Methods:
S.No. Method Description
Add(object) Adds an element at the end of an ArrayList.
Insert(int, object) Inserts an object in the ArrayList at the specified index.
Remove(object) Removes an element from the ArrayList.
RemoveAt(int) Removes an element at the specified index from the ArrayList.
RemoveRange(int index, int count) Removes multiple elements from the specified index till the specified numbers of elements in the ArrayList.
Clear() Removes all the elements from the ArrayList.
Sort() Arranges elements in ascending order.
Reverse() Arranges elements in reverse order according to the index.
Contains() Checks whether specified element exits in the ArrayList or not.

C# ArrayList - Methods

Add elements - ArrayList

Adds an element at the end of an ArrayList.

Example 1: Add elements in ArrayList and display it.

static void Main()
{
    ArrayList list = new ArrayList();
    list.Add(10);
    list.Add(20);
    list.Add("String value");
    list.Add(40.5);
    foreach(var item in list)
    {
        Console.WriteLine(item);
    }
}
10
20
String value
40.5

Insert elements - ArrayList

Use the Insert() method to insert a single item at the specified index.

Example 2: Insert element in arraylist

ArrayList list = new ArrayList();
list.Add(10);
list.Add("Two");
list.Add(30);

list.Insert(1, "Second Item");
list.Insert(2, 100);

foreach (var item in list)
    Console.WriteLine(item);
10
Second Item
100
30

Remove elements - ArrayList

Use the Remove() method to remove a specified element from an ArrayList.

Example 3: Remove element from an arraylist

ArrayList list = new ArrayList();
list.Add(10);
list.Add(20);
list.Add(30);
list.Add(40);

list.Remove(20);

foreach (var item in list)
    Console.WriteLine(item);
10
30
40

Sort elements - ArrayList

Sort() method arrange elements in ascending order. However, all the elements should have same data type otherwise it will throw runtime exception.

Example 4: Sort elements from an arraylist

ArrayList list = new ArrayList();
list.Add(10);
list.Add(5);
list.Add(20);
list.Add(15);

list.Sort();
Console.WriteLine("Ascending Order:");

foreach(var item in list)
    Console.WriteLine(item);
5
10
15
20

Reverse elements - ArrayList

Reverse() method arrange elements in reverse order according to the index of the ArrayList, value in the 0th index appears in last.

Example 5: Reverse elements from an arraylist

ArrayList list = new ArrayList();
list.Add(10);
list.Add(5);
list.Add(20);
list.Add(15);

list.Reverse();
Console.WriteLine("Reverse Order:");

foreach(var item in list)
    Console.WriteLine(item);
15
20
5
10

Contains() method - ArrayList

Contains checks whether specified element exists in the ArrayList or not. Returns true if exists otherwise false.

Example 6: Check existing element in arraylist

ArrayList list = new ArrayList();
list.Add(10);
list.Add(20);
list.Add(30);

Console.WriteLine(list.Contains(10));
Console.WriteLine(list.Contains(50));
true
false
Advertisement

Difference between Array and ArrayList

Difference between Array and ArrayList
S.No. Array ArrayList
Array can store only similar type of data. It is strongly-typed. ArrayList is a non-generic collection type. It is of the object type. So, we can store multiple types of data.
Array stores a fixed number of elements. ArrayList is dynamic in term of capacity. If the number of element exceeds, ArrayList will increase to double its current size.
Array provides better performance than ArrayList. If we are using a large number of ArrayList then it degrades performance because of boxing and unboxing.
Array belongs to namespace System ArrayList belongs to namespace System.Collection
The Array cannot accept null value.An ArrayList can accept null value.

Exercise Question: ArrayList

  1. Input n numbers and add in arraylist.