Open In App

ArrayList in C#

Improve
Improve
Like Article
Like
Save
Share
Report

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:
 

  • The ArrayList class implements the IEnumerable, ICollection, IList, and ICloneable interfaces.
  • The ArrayList class inherits the Object class.
  • The ArrayList is defined under System.Collections namespace. So, when you use Arraylist in your program you must add System.Collections namespace.
  • You cannot implement a multi-dimensional array using ArrayList.
  • The capacity of an ArrayList is the number of elements the ArrayList can hold.
  • You are allowed to use duplicate elements in your ArrayList.
  • You can apply searching and sorting on the elements present in the ArrayList.
  • Arraylist can accept null as a valid value.

 

How to create the ArrayList?

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

  • ArrayList(): This constructor is used to create an instance of ArrayList class which is empty and having no initial capacity.
  • ArrayList(Int32): This constructor is used to create an instance of ArrayList class which is empty and having the specified initial capacity.
  • ArrayList(ICollection): This constructor is used to create an array list initialized with the elements from the specified collection and having the same initial capacity which is copied from collection.

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

  • Step 1: Include System.Collections namespace in your program with the help of using keyword.
    Syntax: 
     
using System.Collections;
  • Step 2: Create an ArrayList using ArrayList class as shown below:
     
ArrayList list_name = new ArrayList();
  • Step 3: If you want to add elements in your ArrayList then use Add() method to add elements in your ArrayList. As shown in the below example.
  • Step 4: The elements of the ArrayList is accessed by using a foreach loop, or by for loop, or by indexer. As shown in the below example we access the ArrayList using a foreach loop.

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:
 

  • Remove: This method is used to remove the first occurrence of a specific object from the ArrayList.
  • RemoveAt: This method is used to remove the element at the specified index of the ArrayList.
  • RemoveRange: This method is used to remove a range of elements from the ArrayList.
  • Clear: This method is used to remove all the elements from the ArrayList.

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: 
 

  • You can also create the object of ArrayList class using the IEnumerable, ICollection, and IList interfaces but some methods of ArrayList class will not be applicable on them as they are not the members of these interfaces.
    Example:
     
// using IList Interface
IList arrlist1 = new ArrayList();

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

// using IEnumerable Interface
IEnumerable arrlist3 = new ArrayList();
  • But here, you cannot apply the methods like Reverse(), AddRange() etc.
  • To read more about the constructors, properties, and methods of ArrayList class please refer to ArrayList Class.
  • To read about the difference between Array and ArrayList please refer Array vs ArrayList in C#.


Last Updated : 19 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads