Home Basics C# ArrayList

C# ArrayList

the ArrayList is a non-generic collection of objects whose size increases dynamically. It is the same as Array except that its size increases dynamically.

An ArrayList can be used to add unknown data where you don’t know the types and the size of the data.

ArrayList Properties

The following are some of the most commonly used properties of an arraylist

Property Description
Capacity It is useful to get or set the number of elements an arraylist can contain.
Count It is useful to get the number of elements in an arraylist.
IsFixedSize It is useful to get a value to indicate that the arraylist has a fixed size or not.
IsReadOnly It is useful to get a value to indicate that the arraylist is read-only or not.
Item It is used to get or set an element at the specified index.
IsSynchronized It is used to get a value to indicate that access to arraylist is synchronized (thread-safe) or not.

ArrayList Methods

The following are some of the commonly used methods of an arraylist to add, search, insert, delete or sort elements of an arraylist.

Method Description
Add It is useful to add an element at the end of the arraylist.
AddRange It is useful to add all the elements of the specified collection at the end of the arraylist.
Clear It will remove all the elements in the arraylist.
Clone It will create a shallow copy of the arraylist.
Contains It is useful to determine whether the specified element exists in an arraylist or not.
CopyTo It is useful to copy all the elements of an arraylist into another compatible array.
GetRange It is useful to return a specified range of arraylist elements starting from the specified index.
IndexOf It is useful to search for a specified element and return a zero-based index if found; otherwise, return -1 if no element is found.
Insert It is useful to insert an element in the arraylist at the specified index.
InsertRange It is useful to insert all the specified collection elements into an arraylist starting from the specified index.
Remove It is useful to remove the first occurrence of a specified element from the arraylist.
RemoveAt It is useful to remove an element from the arraylist based on the specified index position.
RemoveRange It is useful to remove a range of elements from an arraylist.
Reverse It reverses the order of arraylist elements.
Sort It sorts the elements in an arraylist.
ToArray It copies all the elements of an arraylist to a new array object.

Create an ArrayList

The ArrayList class included in the System.Collections namespace. Create an object of the ArrayList using the new keyword.

Example

using System.Collections;

ArrayList arrlist = new ArrayList(); 
// or 
var arrlist = new ArrayList();

Lets look an example of an arraylist

We will add items to the arraylist, we will loop through the items and sort them in this example

 

using System;
using System.Collections;

public class Program
{
	public static void Main()
	{
         ArrayList arrlist = new ArrayList();
         
         Console.WriteLine("Add lottery numbers:");
         arrlist.Add(10);
         arrlist.Add(32);
         arrlist.Add(45);
         arrlist.Add(21);
         arrlist.Add(36);
         arrlist.Add(5);
         
         Console.WriteLine("Capacity: {0} ", arrlist.Capacity);
         Console.WriteLine("Count: {0}", arrlist.Count);
         
         Console.Write("Arraylist items: ");
         foreach (int i in arrlist) 
		 {
            Console.Write(i + " ");
         }
         
         Console.WriteLine();
         Console.Write("Sorted arraylist: ");
         arrlist.Sort();
         foreach (int i in arrlist) 
		 {
            Console.Write(i + " ");
         }
         Console.WriteLine();
         Console.ReadLine();
      }
}

Run this and you should see the following

Add lottery numbers:
Capacity: 8 
Count: 6
Arraylist items: 10 32 45 21 36 5 
Sorted arraylist: 5 10 21 32 36 45

In the example above you can see a couple of useful methods and properties

We are binding different data type values to the newly created arraylist (arrlist) by using  the Add method. We then use the Capacity and Count properties, we can access the number of elements in an arraylist.

We use a foreach loop to loop through the values and then use the sort method to sort the items.

Access ArrayList Elements

We showed how you can access elements of an Arraylist in our precious example, here are some of them

This shows accessing them using index positions, for loop and the foreach loop

using System;
using System.Collections;

public class Program
{
	public static void Main()
	{
         ArrayList arrlist = new ArrayList();
         
         Console.WriteLine("Add lottery numbers:");
         arrlist.Add(10);
         arrlist.Add(32);
         arrlist.Add(45);
         arrlist.Add(21);
         arrlist.Add(36);
         arrlist.Add(5);
         
          // accessing first element
          int number1 = (int)arrlist[0];
          // accessing second element
          int number2 = (int)arrlist[1];
          Console.WriteLine("*********Access Elements using Index Positions********");
          Console.WriteLine("Element at 0: " + number1);
          Console.WriteLine("Element at 1: " + number2);
		
          Console.WriteLine("*********Access Elements using a For Loop********");
          for (int i = 0; i < arrlist.Count; i++)
          {
              Console.WriteLine(arrlist[i]);
          }
          Console.WriteLine("*********Access Elements using a Foreach Loop********");
          foreach (var item in arrlist)
          {
             Console.WriteLine(item);
          }
          Console.ReadLine();
      }
}

Insert Elements

We can insert elements into the arraylist either using Insert() or InsertRange() methods.

 

using System;
using System.Collections;

public class Program
{
	public static void Main()
	{
         ArrayList arrlist = new ArrayList();
         
         Console.WriteLine("Add numbers:");
         arrlist.Add(10);
         arrlist.Add(32);
         arrlist.Add(45);
         arrlist.Add(21);
         arrlist.Add(36);
         arrlist.Add(5);
         foreach (var item in arrlist)
         {
             Console.WriteLine(item);
         }
		 // inserting elements into arraylist
         arrlist.Insert(0, 6);
         arrlist.Insert(1, 49);
         ArrayList arrlist2 = new ArrayList() { 23, 32 };
         // insert arrlist2 into arrlist at position 2
         arrlist.InsertRange(3, arrlist2);
         Console.WriteLine("*********New numbers********");
         foreach (var item in arrlist)
         {
             Console.WriteLine(item);
         }
         Console.ReadLine();
      }
}

When run you should see the following

Add lottery numbers:
10
32
45
21
36
5
*********New lottery numbers********
6
49
10
23
32
32
45
21
3

Remove elements

There a couple of methods to remove an elements from arraylist i.e. either by using Remove() or RemoveAt() or RemoveRange() methods.

 

using System;
using System.Collections;

public class Program
{
	public static void Main()
	{
         ArrayList arrlist = new ArrayList();
         
         Console.WriteLine("Add numbers:");
         arrlist.Add(10);
         arrlist.Add(32);
         arrlist.Add(45);
         arrlist.Add(21);
         arrlist.Add(36);
         arrlist.Add(5);
         foreach (var item in arrlist)
         {
             Console.WriteLine(item);
         }
		 // inserting elements into arraylist
         arrlist.Insert(0, 6);
         arrlist.Insert(1, 49);
         ArrayList arrlist2 = new ArrayList() { 23, 32 };
         // insert arrlist2 into arrlist at position 2
         arrlist.InsertRange(3, arrlist2);
         Console.WriteLine("*********New numbers********");
         foreach (var item in arrlist)
         {
             Console.WriteLine(item);
         }
		 // Removing an element which is having a value 32
         arrlist.Remove(32);
         // Removing an element at index 0
         arrlist.RemoveAt(0);
         // Removing 3 elements starting from index 5
         arrlist.RemoveRange(5, 3);
         Console.WriteLine("*********Numbers********");
         foreach (var item in arrlist)
         {
             Console.WriteLine(item);
         }
         Console.ReadLine();
      }
}

You will see the following

Add numbers:
10
32
45
21
36
5
*********New numbers********
6
49
10
23
32
32
45
21
36
5
*********Numbers********
49
10
23
32
45

Check If Item Exists

We can use the Contains() method, we can check whether the specified element exists in the arraylist or not. In case if it exists, it will return true otherwise false.

using System;
using System.Collections;

public class Program
{
	public static void Main()
	{
         ArrayList arrlist = new ArrayList();
         
         Console.WriteLine("Add numbers:");
         arrlist.Add(10);
         arrlist.Add(32);
         arrlist.Add(45);
         arrlist.Add(21);
         arrlist.Add(36);
         arrlist.Add(5);
         Console.WriteLine("Item Exists: " + arrlist.Contains(40));
		 Console.WriteLine("Item Exists: " + arrlist.Contains(10));
         Console.ReadLine();
      }
}

You will see the following

Add numbers:
Item Exists: False
Item Exists: True

Notes

ArrayList is used to store elements of different data types, and the size of the arraylist can grow or shrink dynamically by adding or removing elements.
The ArrayList class implements the IEnumerable, ICollection, IList, and ICloneable interfaces.
The ArrayList class inherits the Object class.
You need to add a reference to the 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.
There are methods to perform multiple operations like add, insert, delete, sort, reverse, etc., on elements of arraylist.
Arraylist can accept null as a value.

You may also like