ArrayList in C# (original) (raw)

Last Updated : 19 Jan, 2022

ArrayList is a powerful feature of C# language. It is the non-generic type of collection which is defined in System.Collections namespace. It is used to create a dynamic array means the size of the array is increase or decrease automatically according to the requirement of your program, there is no need to specify the size of the ArrayList. Or in other words, ArrayList represents an ordered collection of an object that can be indexed individually. In ArrayList, you can store elements of the same type and of the different types. It belongs to the non-generic collection. The below diagram illustrates the ArrayList class hierarchy:

Important Points:

How to create the ArrayList?

ArrayList class has three constructors which are used to create the ArrayList.

Let's see how to create an ArrayList using ArrayList() constructor:

using System.Collections;

ArrayList list_name = new ArrayList();

Example: Below program show how to create an ArrayList, adding elements to the ArrayList, and how to access the elements of the ArrayList.

CSharp `

// C# program to demonstrate the ArrayList using System; using System.Collections;

class GFG {

// Main Method
static public void Main()
{

    // Creating ArrayList
    ArrayList My_array = new ArrayList();

    // Adding elements in the 
    // My_array ArrayList
    // This ArrayList contains elements
    // of different types
    My_array.Add(12.56);
    My_array.Add("GeeksforGeeks");
    My_array.Add(null);
    My_array.Add('G');
    My_array.Add(1234);

    // Accessing the elements 
    // of My_array ArrayList
    // Using foreach loop
    foreach(var elements in My_array)
    {
        Console.WriteLine(elements);
    }
}

}

`

Output:

12.56 GeeksforGeeks

G 1234

How to find the Capacity and Count of elements of the ArrayList?

To find this we can use Count and Capacity property of an ArrayList class as follows:
Example:

csharp `

// C# program to find the number of // elements and capacity of ArrayList using System; using System.Collections;

class GFG {

// Driver code public static void Main() {

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

// Adding elements to ArrayList 
myList.Add(1); 
myList.Add(2); 
myList.Add(3); 
myList.Add(4); 
myList.Add(5); 

// Displaying count of elements of ArrayList 
Console.WriteLine("Number of elements: " + myList.Count); 

// Displaying Current capacity of ArrayList 
Console.WriteLine("Current capacity: " + myList.Capacity); 

} }

`

Output:

Number of elements: 5 Current capacity: 8

How to remove elements from the ArrayList?

In ArrayList, you are allowed to remove elements from the ArrayList. ArrayList provides four different methods to remove elements and the methods are:

Example:

CSharp `

// C# program to illustrate how // to remove elements from the // ArrayList using System; using System.Collections;

class GFG {

static public void Main()
{

    // Creating ArrayList
    ArrayList My_array = new ArrayList();

    // Adding elements in the 
    // My_array ArrayList
    // This ArrayList contains elements
    // of the same types
    My_array.Add('G');
    My_array.Add('E');
    My_array.Add('E');
    My_array.Add('K');
    My_array.Add('S');
    My_array.Add('F');
    My_array.Add('O');
    My_array.Add('R');
    My_array.Add('G');
    My_array.Add('E');
    My_array.Add('E');
    My_array.Add('K');
    My_array.Add('S');

    Console.WriteLine("Initial number of elements : " 
                                   + My_array.Count);

    // Remove the 'G' element 
    // from the ArrayList
    // Using Remove() method
    My_array.Remove('G');
    Console.WriteLine("After Remove() method the "+
          "number of elements: " + My_array.Count);

    // Remove the element present at index 8
    // Using RemoveAt() method
    My_array.RemoveAt(8);
    Console.WriteLine("After RemoveAt() method the "+
            "number of elements: " + My_array.Count);

    // Remove 3 elements starting from index 1
    // using RemoveRange() method
    My_array.RemoveRange(1, 3);
    Console.WriteLine("After RemoveRange() method the"+
             " number of elements: " + My_array.Count);

    // Remove the all element 
    // present in ArrayList
    // Using Clear() method
    My_array.Clear();
    Console.WriteLine("After Clear() method the "+
        "number of elements: " + My_array.Count);
}

}

`

Output:

Initial number of elements : 13 After Remove() method the number of elements: 12 After RemoveAt() method the number of elements: 11 After RemoveRange() method the number of elements: 8 After Clear() method the number of elements: 0

How to sort the elements of the ArrayList?

In ArrayList, you can perform sorting on the elements present in the given ArrayList using the Sort() method. This method uses the QuickSort algorithm to perform sorting on the ArrayList and the elements are arranged in ascending order. The use of this method is shown in the below example.
Example:

CSharp `

// C# program to illustrate // sorting of ArrayList using System; using System.Collections;

public class GFG { static public void Main() {

    // Creating ArrayList
    ArrayList My_array = new ArrayList();

    // Adding elements in the 
    // My_array ArrayList
    // This ArrayList contains 
    // elements of the same types
    My_array.Add(1);
    My_array.Add(6);
    My_array.Add(40);
    My_array.Add(10);
    My_array.Add(5);
    My_array.Add(3);
    My_array.Add(2);
    My_array.Add(4);

    // ArrayList before sorting
    Console.WriteLine("ArrayList before using Sort() method:");

    foreach(var elements in My_array)
    {
        Console.WriteLine(elements);
    }

    // Sort the elements of the ArrayList
    // Using sort() method
    My_array.Sort();

    // ArrayList after sorting
    Console.WriteLine("ArrayList after using Sort() method:");
    foreach(var elements in My_array)
    {
        Console.WriteLine(elements);
    }
}

}

`

Output:

ArrayList before using Sort() method: 1 6 40 10 5 3 2 4 ArrayList after using Sort() method: 1 2 3 4 5 6 10 40

Note:

// using IList Interface IList arrlist1 = new ArrayList();

// using ICollection Interface ICollection arrlist2 = new ArrayList();

// using IEnumerable Interface IEnumerable arrlist3 = new ArrayList();