ArrayList in C#

ArrayList is a powerful feature of the C# language. This is a non-generic collection type defined in the System. Collections namespace. Used to create dynamic arrays. This means that the size of the array will automatically grow or shrink depending on your program’s needs. There is no need to specify the size of the ArrayList. In other words, an ArrayList represents an ordered collection of objects that can be indexed individually. An ArrayList can contain elements of the same type and elements of different types.
This belongs to a non-generic collection.

important Points of ArrayList:

  • The ArrayList class implements the IEnumerableICollectionIList, 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 given below:

  • ArrayList(): This constructor is used to create an instance of the ArrayList class that is empty and has no initial capacity.
  • ArrayList(Int32): This constructor is used to create an instance of the ArrayList class with an empty and specified initial capacity.
  • ArrayList(ICollection): This constructor is used to create an array list that is initialized with the elements of the specified collection and has the same initial capacity as copied from the collection.
using System.Collections;
ArrayList list_name = new ArrayList();

In below program let’s check how to create an ArrayList, adding elements to the ArrayList, and how to access the elements of the ArrayList.

using System;
using System.Collections;
 
class CodeConfig {

    static public void Main()
    {
        ArrayList arr = new ArrayList();
        arr.Add(10.56);
        arr.Add("CodeConfig.in");
        arr.Add(code);
        arr.Add('India');
        arr.Add(101);
 
        // Now Accessing the elements

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

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

using System;
using System.Collections;
 
class CodeConfig{
public static void Main() {
     
    // Creating an ArrayList and adding element to it.
    ArrayList myLst = new ArrayList();
    myLst.Add(101);
    myLst.Add(102);
    myLst.Add(103);
    myLst.Add(104);
    myLst.Add(105);
     
    // Displaying count of elements within ArrayList
    Console.WriteLine("Number of elements: " + myLst.Count);
     
    // Displaying Current capacity of ArrayList
    Console.WriteLine("Current capacity: " + myLst.Capacity);
}
}

How to remove elements from the ArrayList?

ArrayList provides 4 different methods to remove elements and the methods are as given below:

  • RemoveThis method is used to remove the first occurrence of a specific object from the ArrayList.
  • RemoveAtThis method is used to remove the element at the specified index of the ArrayList.
  • RemoveRangeThis 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.
using System;
using System.Collections;
 
class CodeConfig
{
public static void Main() {
     
    // Creating an ArrayList and adding element to it.
    ArrayList myLst = new ArrayList();
    myLst.Add(101);
    myLst.Add(102);
    myLst.Add(103);
    myLst.Add(104);
    myLst.Add(105);
     
 myLst.Remove('102');        // Remove the '102' element.
 myLst.RemoveAt(3);          // Remove the element present at index 3
 myLst.RemoveRange(1, 3);    // Remove 3 elements starting from index 1
 myLst.Clear();              // Remove all the elements present in ArrayList.
 }
}

How to sort the elements of the ArrayList?

ArrayList allows you to sort the elements present in a given ArrayList using the Sort() method. This method sorts the ArrayList using the QuickSort algorithm, placing the elements in ascending order. The following example shows how to use this method.

using System;
using System.Collections;
 
class CodeConfig
{
public static void Main() {
     
    // Creating an ArrayList and adding element to it.
    ArrayList myLst = new ArrayList();
    myLst.Add(101);
    myLst.Add(104);
    myLst.Add(105);
    myLst.Add(102);
    myLst.Add(103);
     
     // ArrayList before sorting
       foreach(var elements in myLst)
        {
            Console.WriteLine(elements);
        }

      // Using sort() method
        myLst.Sort();       

      // ArrayList after sorting
        foreach(var elements in myLst)
        {
            Console.WriteLine(elements);
        }

 }
}

In this article we tried to explain ArrayList in C# with Examples. I hope you enjoyed reading this article. For more information you can check out Microsoft learn.

For more information you can visit our C# Section for more such articles.

Leave a Reply

Your email address will not be published. Required fields are marked *