C# ArrayList Collection Tutorial

In this tutorial we learn about the arraylist standard collection. A complex data container that is similar to an array but accepts different data types and is dynamic in size.

What is an ArrayList

An ArrayList in C# is similar to a basic array. Where an array has a fixed size and can store only a single type of value, an ArrayList has a dynamic size and can stored any type of value.

We use an ArrayList when we don’t know ahead of time how many objects will be stored. Or, when we need to store different types.

The ArrayList Namespace

ArrayList, as a non-generic collection class, is defined in the System.Collections namespace. Before we can use ArrayLists, we must specify this namespace at the top of our document.

Example:
using System;
using System.Collections;

How to declare an ArrayList

To declare an Arraylist we use the keyword ArrayList , followed by the list name and assignment operator. Then, we specify the new keyword followed by the ArrayList() function.

Syntax:
 ArrayList list_name = new ArrayList();

The new keyword will create a new instance of the ArrayList class and store it in the list_name object.

Example:
using System.Collections;

namespace ArrayLists
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList numbers = new ArrayList();
        }
    }
}

Typically, we don’t specify the size of an ArrayList as we would with a basic array. However, it can be done through the constructor and may save on system resources.

Just like with a standard class, we can pass values to the ArrayList constructor through its parameters in between the parentheses.

How to initialize an ArrayList

If we know ahead of time which values we want to store inside an ArrayList, we can use initialization syntax.

Initialization syntax is where we create a code block for the ArrayList function and add our values inside, separated by a comma.

Syntax:
 ArrayList list_name = new ArrayList() { value_1, value_2 };
Example:
using System.Collections;

namespace ArrayLists
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList numbers = new ArrayList() { 1, 2, 3, 4, 5 };
        }
    }
}

As mentioned earlier, ArrayLists support different types. We can have integers, floats, strings etc. all in the same ArrayList.

Example:
using System.Collections;

namespace ArrayLists
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList aList = new ArrayList()
            {
                10,
                3.14,
                "Hello",
                "World"
            };
        }
    }
}

In the example above, we initialize an ArrayList with an int, float and two strings.

Multiple different types in the same ArrayList is the main benefit that an ArrayList has over a standard array.

How to access single elements in an ArrayList with the indexer

To access a single element in an ArrayList, we specify that element’s index between open and close square brackets. This is called the indexer.

Syntax:
 list_name[index];
Example:
using System;
using System.Collections;

namespace ArrayLists
{
    class Program
    {
        static void Main()
        {
            ArrayList aList = new ArrayList() { 10, 3.14, "Hello", "World" };

            Console.WriteLine(aList[2]);
            Console.ReadLine();
        }
    }
}

In the example above, we access the value at index[2], which is the word “Hello”.

As we’ve mentioned in the arrays tutorial, any numerical index always start at the number 0, not at 1. That’s why index[2] is the word “Hello” and not 3.14.

How to access values in an ArrayList with a loop

Because we don’t necessarily know how many elements the ArrayList has, we can use the foreach loop to go through each element until it reaches the end.

Example:
using System;
using System.Collections;

namespace ArrayLists
{
    class Program
    {
        static void Main()
        {
            ArrayList aList = new ArrayList() { 10, 3.14, "Hello", "World" };

            foreach(var e in aList)
                Console.WriteLine(e);

            Console.ReadLine();
        }
    }
}

In the example above, we iterate through the ArrayList in a foreach loop, storing the value in a temporary variable e. For simplicity, we only print out the value to the console.

How to add a value to an ArrayList

Because an ArrayList is dynamic in size, we need a way to add values to it.

To add a value to an ArrayList, we use the .Add() function. The function will add anything we input in the parentheses to the end of the list.

Syntax:
 list_name.Add(value);
Example:
using System;
using System.Collections;

namespace ArrayLists
{
    class Program
    {
        static void Main()
        {
            ArrayList aList = new ArrayList() { 1, 3.14, "Hello", "World" };

            aList.Add(true);

            foreach(var e in aList)
                Console.WriteLine(e);

            Console.ReadLine();
        }
    }
}

In the example above, we added the boolean value True to the end of the ArrayList.

How to add multiple values to an ArrayList

We don’t have to settle for adding values one by one with the Add() function. We can add multiple values with the AddRange() function.

Syntax:
list_name.AddRange( list_of_values );

To add multiple values we use an array, or list of values, as the input for the AddRange() function.

Example:
using System;
using System.Collections;

namespace ArrayLists
{
    class Program
    {
        static void Main()
        {
            var aList1 = new ArrayList() { 1, 2, 3, 4 };
            var aList2 = new ArrayList() { "Five", "Six", "Seven" };

            aList1.AddRange(aList2);

            foreach(var e in aList1)
                Console.WriteLine(e);

            Console.ReadLine();
        }
    }
}

In the example above, we add all the values from aList2 into aList1. The AddRange() function will also add new values to the end of the list.

We can also add to an ArrayList inline, right inside the AddRange() function.

Example:
using System;
using System.Collections;

namespace ArrayLists
{
    class Program
    {
        static void Main()
        {
            var aList = new ArrayList() { 1, 2, 3, 4 };

            aList.AddRange(new ArrayList() { "Five", "Six", "Seven" });

            foreach(var e in aList)
                Console.WriteLine(e);

            Console.ReadLine();
        }
    }
}

In the example above we initialize a new ArrayList right inside the AddRange() function. It works exactly the same as adding a predefined list to the AddRange() function’s parameter.

Summary: Points to remember

  • An ArrayList is a collection of different types of items.
  • The difference between an ArrayList and an array is that an ArrayList is dynamic in size and supports various element types.
  • An ArrayList can be initialized with initialization syntax. That’s to say, comma separated values in a code block.
  • We can access elements in an ArrayList with the indexer, or with loops.
  • Because an ArrayList is dynamic in size, we use the Add() and AddRange() functions to append new items to the list.
    • The Add() function will add a single value to the end of the ArrayList.
    • The AddRange() will add multiple items, in the form of an array or collection, to the end of the ArrayList.