C# ArrayList Class

The comprehensive tutorial to C# ArrayList for beginners and experts

What is C# ArrayList Class?

A few important points to learn about ArrayList Class before we go into its methods and examples:

  • C# ArrayList Class can be defined as an ordered collection of objects.
  • The ArrayList belongs to the System.Collections namespace.
  • ArrayList is non-generic unlike the List<T> which is generic.
  • You may compare ArrayList to arrays – the ArrayList is the sophisticated form of an array.
  • The array is the fixed size whereas ArrayList can grow automatically (as required).
  • The ArrayList can be one dimensional, unlike the arrays that can be multi-dimensional.

How to create an ArrayList?

The ArrayList can be created by using the new keyword as follows:

ArrayList ArrLstEx = new ArrayList();

You can see, the ArrayList is defined without specifying the size and type. That means you may add as many items as you want and ArrayList may contain different types of objects. To make it clear, see an example below for defining a List of int type:

List<int> list_name = new List<int>();

A string type list example:

List<string> list_string = new List<string>();

Similarly, this is how an array is declared and initialized:

int[] int_Arr = new int[5];

Now, as you should understand the basic difference let us move to show you examples of C# ArrayList and its commonly used methods.

The example of creating a numeric items ArrayList

In the first example of ArrayList, I will be declaring, initializing, and iterating through the ArrayList.

For this ArrayList, I only used the numeric items whose data type is int.

A foreach statement is used to iterate through the ArrayList, have a look:

using System;

using System.Collections;

class ArrayList_Demo

{
    static void Main()

    {

        ArrayList numArrLlst = new ArrayList();

        numArrLlst.Add(10);
        numArrLlst.Add(20);
        numArrLlst.Add(30);
        numArrLlst.Add(40);
        numArrLlst.Add(50);

        foreach (int temp in numArrLlst)
        {
            System.Console.WriteLine("ArrayList Item = " + temp);
        }

        Console.ReadLine();
    }
}

Output:

ArrayList Item = 10
ArrayList Item = 20
ArrayList Item = 30
ArrayList Item = 40
ArrayList Item = 50
Did you know? The List<T> was introduced in C# 2.0 which is type-safe. The List<T> includes almost all methods as provided for ArrayList. For any new developments (C# version 2 or +), you should only use List<T> rather than ArrayList.

An example of mixed item ArrayList

The real difference and usage of ArrayList (than List) is its ability to hold various datatype objects. For example, a single ArrayList may contain numeric, float, string items, etc.

The following example shows using a float, int, double, and a string in a single ArrayList.

See the code and output below:

using System;

using System.Collections;

class ArrayList_Demo

{
    static void Main()
    {
        ArrayList MixedArrlst = new ArrayList();

        MixedArrlst.Add(10.12);
        MixedArrlst.Add(20020014124111);
        MixedArrlst.Add(30.14245474574);
        MixedArrlst.Add("Some String");
        MixedArrlst.Add(50);

        foreach (var temp in MixedArrlst)
        {
            System.Console.WriteLine("ArrayList Item = " + temp);
        }
        Console.ReadLine();
    }
}

The result:

ArrayList Item = 10.12
ArrayList Item = 20020014124111
ArrayList Item = 30.14245474574
ArrayList Item = Some String
ArrayList Item = 50

Notice that I used var this time in the foreach statement as compared to int in the above example.

Few commonly used methods of ArrayList

In this section, I will explain a few commonly used methods of ArrayList in C#. These methods include:

  • Add
  • AddRange
  • Insert
  • InsertRange
  • Clear
  • Remove
  • Sort

ArrayList Add method

We have already used the Add method in the above examples.

The Add method is used to insert a new element at the end of an ArrayList. For example:
        ArrayList ArrLlst = new ArrayList();

        ArrLlst.Add(10);

        ArrLlst.Add(15.55);

        ArrLlst.Add(“A String item”);

In the output of the above examples, you can see the items are displayed in the same order as we added by using the Add method.

The AddRange method example

As the name suggests, the AddRange method is used to add a range of elements to an existing ArrayList.

  • You may add any ICollection at the end of an existing ArrayList by using the AddRange method. For example, adding one ArrayList to another.
  • Just like the Add method, the AddRange also adds new elements at the end of the ArrayList.
  • In the following example, we have created two ArrayLists.
  • Both contain numeric items while we will use the AddRange method to add the items of ArrayList2 to ArrayList1:
using System;

using System.Collections;



class ArrayList_Demo_AddRange

{

    static void Main()

    {
        ArrayList Arrlst_Range = new ArrayList();

        Arrlst_Range.Add(11);
        Arrlst_Range.Add(22);
        Arrlst_Range.Add(33);

        ArrayList Arrlst_Range2 = new ArrayList();
        Arrlst_Range2.Add(12);
        Arrlst_Range2.Add(24);
        Arrlst_Range2.Add(36);

        Arrlst_Range.AddRange(Arrlst_Range2);

        foreach (var temp in Arrlst_Range)
        {
            System.Console.WriteLine("ArrayList Item = " + temp);
        }

        Console.ReadLine();
    }
}

The result:

ArrayList Item = 11
ArrayList Item = 22
ArrayList Item = 33
ArrayList Item = 12
ArrayList Item = 24
ArrayList Item = 36

Adding elements by Insert method

If you require adding an element at the specified index rather than at the end of an ArrayIndex then use the Insert method.

See an example below where I added a string element at the 4 index position:

using System;

using System.Collections;
class ArrayList_Demo_Insert

{

    static void Main()

    {
        ArrayList Arrlst_Insert = new ArrayList();
        Arrlst_Insert.Add(5);
        Arrlst_Insert.Add(11);
        Arrlst_Insert.Add(17);
        Arrlst_Insert.Add(33);
        Arrlst_Insert.Add(44);
        Arrlst_Insert.Add(55);

        foreach (var temp in Arrlst_Insert)
       {
            System.Console.WriteLine("ArrayList Item = " + temp);
        }

        Arrlst_Insert.Insert(4, "A String at specified pos");

        System.Console.WriteLine("\n*******After Insert Method******\n");

        foreach (var temp in Arrlst_Insert)
        {
            System.Console.WriteLine("ArrayList Item = " + temp);
        }

        Console.ReadLine();
    }
}

Output:

ArrayList Item = 5
ArrayList Item = 11
ArrayList Item = 17
ArrayList Item = 33
ArrayList Item = 44
ArrayList Item = 55
*******After Insert Method******
ArrayList Item = 5
ArrayList Item = 11
ArrayList Item = 17
ArrayList Item = 33
ArrayList Item = A String at specified pos
ArrayList Item = 44
ArrayList Item = 55

c# Arraylist-Insert

The InsertRange method

You may use the InsertRange method for adding new elements from other Collections to the specified position, unlike the AddRange method that adds new elements at the end.

To illustrate that, suppose we have two array lists.

This is how InsertRange method is used to insert the second ArrayList into the first at 1 index position:

using System;

using System.Collections;

class ArrayList_Demo_InsertRange

{
    static void Main()
    {
        ArrayList Arrlst_InsertRange = new ArrayList();

        Arrlst_InsertRange.Add(1);
        Arrlst_InsertRange.Add(2);
        Arrlst_InsertRange.Add(3);

        ArrayList Arrlst_InsertRange2 = new ArrayList();
        Arrlst_InsertRange2.Add("a");
        Arrlst_InsertRange2.Add("b");
        Arrlst_InsertRange2.Add("c");

        Arrlst_InsertRange.InsertRange(1, Arrlst_InsertRange2);

        foreach (var temp in Arrlst_InsertRange)
        {
            System.Console.WriteLine("ArrayList Item = " + temp);
        }
        Console.ReadLine();
    }
}

The Result:

c# Arraylist-InsertRange

The example of ArrayList Clear method

To empty an ArrayList, you may use the Clear method. It will remove all elements from the ArrayList.

See a simple example of using the Clear method below:

using System;

using System.Collections;
class ArrayList_Demo_Clear

{
    static void Main()
    {
        ArrayList Arrlst_Clear = new ArrayList();

        Arrlst_Clear.Add(1);
        Arrlst_Clear.Add(2);
        Arrlst_Clear.Add(3);

        Arrlst_Clear.Clear();

        foreach (var temp in Arrlst_Clear)
        {
            System.Console.WriteLine("ArrayList Item = " + temp);
        }
        Console.ReadLine();
    }
}

The output should be an empty ArrayList.

Using Remove method of ArrayList

  • For deleting a single element from the ArrayList based on the specified element, use the Remove method.
  • In the Remove method, you will provide the item to be removed from the ArrayList.
  • If the ArrayList contains duplicate elements then only the first occurrence is deleted by the Remove() method – as shown in the example below:
using System;

using System.Collections;

class ArrayList_Demo_Remove
{
    static void Main()
    {
        ArrayList Arrlst_Remove = new ArrayList();

        Arrlst_Remove.Add(1);
        Arrlst_Remove.Add(2);
        Arrlst_Remove.Add(3);
        Arrlst_Remove.Add(3);
        Arrlst_Remove.Add(1);
        Arrlst_Remove.Add(2);

        Arrlst_Remove.Remove(2);

        foreach (var temp in Arrlst_Remove)
        {
            System.Console.WriteLine("ArrayList Item = " + temp);
        }

        Console.ReadLine();
    }
}

The Result:

ArrayList Item = 1
ArrayList Item = 3
ArrayList Item = 3
ArrayList Item = 1
ArrayList Item = 2

You can see the second occurrence of item “2” still exists.

How to sort an ArrayList?

To sort an ArrayList, you may use the sort() method. See an example where we have a numeric ArrayList with random numbers.

The ArrayList is displayed before and after using the sort() method:

using System;

using System.Collections;

class ArrayList_Demo_Sort

{

    static void Main()

    {
        ArrayList Arrlst_Sort = new ArrayList();

        Arrlst_Sort.Add(5);
        Arrlst_Sort.Add(17);
        Arrlst_Sort.Add(15);
        Arrlst_Sort.Add(11);
        Arrlst_Sort.Add(21);
        Arrlst_Sort.Add(3);

        System.Console.WriteLine("******Before Sort() method*****\n");
        foreach (var temp in Arrlst_Sort)

        {
            System.Console.WriteLine("ArrayList Item = " + temp);
        }
        Arrlst_Sort.Sort();

        System.Console.WriteLine("\n******After Sort() method*****\n");

        foreach (var temp in Arrlst_Sort)
        {
            System.Console.WriteLine("ArrayList Item = " + temp);
        }
        Console.ReadLine();
    }
}

c# Arraylist-Sort

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!