ArrayList in C#

In this tutorial, learn C# ArrayList class and most commonly asked questions about C# ArrayList.

C# ArrayList is a non-generic collection. The ArrayList class represents an array list and it can contain elements of any data types. The ArrayList class is defined in the System.Collections namespace. An ArrayList is dynamic array and grows automatically when new items are added to the collection.
 
This tutorial explains basics of C# ArrayList with most common code examples. 
 

1. Why C# ArrayList 

 
C# array is a collection of data types.  For example, you can create an array for student marks. The marks are an integer value so you can create an integer array that holds all student marks. Suppose some students have marks from an examination but other students have a grade. A grade is a string value like “A”, ”B” and so on. What do you? You can create two arrays, one for marks that is an integer array and another for grade that is a string array. You have two arrays so you can’t retrieve the result in students sequence because you have marks in the first array then grades in the second array. So when you have multiple types set then you can use an ArrayList. ArrayList is one of the most flexible data structures from C# Collections. An ArrayList contains a simple list of values.
  1. To use an ArrayList you need to add a System.Collections namespace.
  2. ArrayList implements the System.Collections.IList interface using an array whose size is dynamically increased as required.
  3. ArrayList is an alternate of an Array but you can’t implement multi-dimensional arrays using ArrayList.
  4. Array is a fixed size collection whereas an ArrayList's size can be dynamically increased or decreased.
  5. An Array is a collection of variables of the same type whereas an ArrayList is a collection of variables of the same type or multiple types.
  6. The capacity of an ArrayList is the number of elements the ArrayList can hold. As elements are added to an ArrayList, the capacity is automatically increased as required through reallocation. The capacity can be decreased by calling TrimToSize or by setting the Capacity property explicitly.
  7. Elements in an ArrayList collection can be accessed using an integer index. Indexes in this collection are zero-based.
  8. The ArrayList collection accepts null as a valid value, an allows duplicate elements.

2. How to add an item to a C# ArrayList

The Add method on ArrayList appends a new item/element object to the end of the ArrayList. You can add elements in the ArrayList until memory runs out. The objects are stored in the managed heap. Let’s see an example of creating an ArrayList and add two elements using the Add() method of ArrayList.

  1. using System;  
  2. using System.Collections;  
  3.   
  4. namespace ArrayListApplication  
  5. {  
  6.    class Program  
  7.     {  
  8.        static void Main(string[] args)  
  9.         {  
  10.            ArrayList resultList = new ArrayList();  
  11.             resultList.Add(90);  
  12.             resultList.Add(95);  
  13.            Console.Read();  
  14.         }  
  15.     }  
  16. }   
Whenever you execute this program you will get that your ArrayList has been populated by elements as per Figure 1.1.

 Add item in ArrayList
Figure 1.1 Adds item in ArrayList.

3. How to read elements from an ArrayList

You can read items from the ArrayList using a foreach loop. Let’s see an example.

  1. using System;  
  2. using System.Collections;  
  3.   
  4. namespace ArrayListApplication  
  5. {  
  6.    class Program  
  7.     {  
  8.        static void Main(string[] args)  
  9.         {  
  10.            ArrayList personList= new ArrayList();  
  11.             personList.Add("Sandeep");  
  12.             personList.Add("Raviendra");  
  13.            foreach (var item in personList)  
  14.             {  
  15.                string arrayItem = string.Format($"Name  is {item}");  
  16.                Console.WriteLine(arrayItem);  
  17.             }  
  18.            Console.Read();  
  19.         }  
  20.     }  
  21. }   
You execute this program then get output as in Figure 1.2.
 
Read Data from ArrayList 
Figure 1.2 Read Data from an ArrayList 

ArrayList items are stored on an index basis. Let’s see Figure 1.1 where the first item stored is at index 0 whereas the next item stored at the next index, in other words, index 1 so you can also retrieve items from an ArrayList using an index. Let’s see the following code.

  1. using System;  
  2. using System.Collections;  
  3.   
  4. namespace ArrayListApplication  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             ArrayList personList = new ArrayList();  
  11.             personList.Add("Sandeep");  
  12.             personList.Add("Raviendra");  
  13.             for (int i = 0; i < personList.Count; i++)  
  14.             {  
  15.                 string arrayItem = string.Format($"Name is {personList[i]}");  
  16.                 Console.WriteLine(arrayItem);  
  17.             }  
  18.             Console.Read();  
  19.         }  
  20.     }  
  21. } 
When you execute this program you will get the same output as in Figure 1.2. One more point is that you can read items from an ArrayList by an index but you can’t add items in an ArrayList by index.

4. How to insert an Item to a C# ArrayList

You have seen that you can add items to an ArrayList so what is meant by "insert"? The Add method adds an item to the end of the ArrayList whereas Insert adds an item at a specific index position. Let’s see an example where I add an item to index 1.

  1. using System;  
  2. using System.Collections;  
  3.   
  4. namespace ArrayListApplication  
  5. {  
  6.    class Program  
  7.     {  
  8.        static void Main(string[] args)  
  9.         {  
  10.            ArrayList personList = new ArrayList();  
  11.             personList.Add("Sandeep");  
  12.             personList.Add("Raviendra");  
  13.            Console.WriteLine("=====Original List======");  
  14.            for (int i = 0; i < personList.Count; i++)  
  15.             {  
  16.                 Console.WriteLine(arrayItem);  
  17.            }  
  18.             personList.Insert(1,"Shaijal");  
  19.            Console.WriteLine("=====Modify List======");  
  20.            for (int i = 0; i < personList.Count; i++)  
  21.             {  
  22.                Console.WriteLine(arrayItem);  
  23.             }  
  24.            Console.Read();  
  25.         }  
  26.     }  
  27. }   
Now you can execute this program and get the results as in Figure 1.3.
 
Insert an Item in ArrayList 
Figure 1.3 Insert an item at index 1

5. How to remove an item from an ArrayList

You can remove an item or items from an ArrayList. The ArrayList class provides four methods to remove an item or items from it. These methods are:

  1. Remove
  2. RemoveAt
  3. RemoveRange
  4. Clear

Let’s describe each one by one.

1.  Remove

The Remove() method has one parameter that is the object type. You know that each item of the ArrayList is an object type so you need to pass that item in the Remove method to remove that item from the ArrayList. Let’s see an example where the first item is removed from the ArrayList.
  1. using System;  
  2. using System.Collections;  
  3.   
  4. namespace ArrayListApplication  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             ArrayList personList = new ArrayList();  
  11.             personList.Add("Sandeep");  
  12.             personList.Add("Raviendra");  
  13.             personList.Add("Shaijal");  
  14.             Console.WriteLine("=====Original List======");  
  15.             for (int i = 0; i < personList.Count; i++)  
  16.             {  
  17.                 string arrayItem = string.Format($"Name  is {personList[i]}");  
  18.                 Console.WriteLine(arrayItem);  
  19.             }  
  20.             //remove first item from person list using index  
  21.             personList.Remove("Sandeep");  
  22.             Console.WriteLine("=====Modified List======");  
  23.             for (int i = 0; i < personList.Count; i++)  
  24.             {  
  25.                 string arrayItem = string.Format($"Name  is {personList[i]}");  
  26.                 Console.WriteLine(arrayItem);  
  27.             }  
  28.             Console.Read();  
  29.         }  
  30.     }  
  31. }  
You execute this program then get the results as in Figure 1.4.
 
Remove an Item from ArrayList 
Figure 1.4 Remove an item from the ArrayList

2. RemoveAt

In the previous method you saw that you can remove an item from the ArrayList using the Remove method. Notice one thing, that you need to remember the item to be removed. Suppose you have an ArrayList of complex objects that time you can’t remember the state of an object that will be removed. In this scenario, you can use the RemoveAt method of which you only need to pass the index number of the item. Let’s see an example of removing the first item of the ArrayList using index position.
  1. using System;  
  2. using System.Collections;  
  3.   
  4. namespace ArrayListApplication  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             ArrayList personList = new ArrayList();  
  11.             personList.Add("Sandeep");  
  12.             personList.Add("Raviendra");  
  13.             personList.Add("Shaijal");  
  14.             Console.WriteLine("=====Original List======");  
  15.             for (int i = 0; i < personList.Count; i++)  
  16.             {  
  17.                 string arrayItem = string.Format($"Name  is {personList[i]}");  
  18.                 Console.WriteLine(arrayItem);  
  19.             }  
  20.             //remove first item from person list using index  
  21.             personList.RemoveAt(0);  
  22.             Console.WriteLine("=====Modified List======");  
  23.             for (int i = 0; i < personList.Count; i++)  
  24.             {  
  25.                 string arrayItem = string.Format($"Name  is {personList[i]}");  
  26.                 Console.WriteLine(arrayItem);  
  27.             }  
  28.             Console.Read();  
  29.         }  
  30.     }  
  31. }  
When you execute this program you get the same result as in Figure 1.4.

3. RemoveRange

Each of the previous two methods removed a single item at a time. Suppose you want to remove more than one item from the ArrayList, then you need to call these methods multiple times. Alternatively you can use the RemoveRange method that can remove multiple items in a single call. The RemoveRange method has two arguments. The first argument is the beginning index number to be removed and the second parameter is the count yo be removed, in other words how many items you want to remove. But it removes items in sequence. Let’s see an example where the first two items are removed.

  1. using System;  
  2. using System.Collections;  
  3.   
  4. namespace ArrayListApplication  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             ArrayList personList = new ArrayList();  
  11.             personList.Add("Sandeep");  
  12.             personList.Add("Raviendra");  
  13.             personList.Add("Shaijal");  
  14.             Console.WriteLine("=====Original List======");  
  15.             for (int i = 0; i < personList.Count; i++)  
  16.             {  
  17.                 string arrayItem = string.Format($"Name  is {personList[i]}");  
  18.                 Console.WriteLine(arrayItem);  
  19.             }  
  20.             //remove first two item from person list using index  
  21.             personList.RemoveRange(0, 2);  
  22.             Console.WriteLine("=====Modified List======");  
  23.             for (int i = 0; i < personList.Count; i++)  
  24.             {  
  25.                 string arrayItem = string.Format($"Name  is {personList[i]}");  
  26.                 Console.WriteLine(arrayItem);  
  27.             }  
  28.             Console.Read();  
  29.         }  
  30.     }  
  31. }  
When you execute this program you get the output as in Figure 1.5.

RemoveRange ArrayList
Figure 1.5 Remove more than one items from the ArrayList

Before proceeding, I want to tell you that the ArrayList class has a property Count that represents how many items are in the ArrayList. Let’s see an example.

  1. using System;  
  2. using System.Collections;  
  3.   
  4. namespace ArrayListApplication  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             ArrayList personList = new ArrayList();  
  11.             personList.Add("Sandeep");  
  12.             personList.Add("Raviendra");  
  13.             personList.Add("Shaijal");  
  14.             int totalItems = personList.Count;  
  15.             Console.WriteLine(string.Format($"Total Number Of Items in ArrayList: {totalItems}"));  
  16.             Console.Read();  
  17.         }  
  18.     }  
  19. }  
You run this program then get the results as in Figure 1.6.

count items in ArrayList

Figure 1.6 Total number of items in the ArrayList 

4. Clear

The Clear method of ArrayList removes all the items from the ArrayList but doesn’t reduce the capacity of the ArrayList. The Clear method removes all items from the ArrayList. Let’s see an example.

  1. using System;  
  2. using System.Collections;  
  3.   
  4. namespace ArrayListApplication  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             ArrayList personList = new ArrayList();  
  11.             personList.Add("Sandeep");  
  12.             personList.Add("Raviendra");  
  13.             personList.Add("Shaijal");  
  14.             int totalItems = personList.Count;  
  15.             Console.WriteLine(string.Format($"Total Number Of Items in ArrayList: {totalItems}"));  
  16.             //Remove all items from person list             
  17.             personList.Clear();  
  18.   
  19.             totalItems = personList.Count;  
  20.             Console.WriteLine(string.Format($"Total Number Of Items in ArrayList: {totalItems}"));  
  21.             Console.Read();  
  22.   
  23.         }  
  24.     }  
  25. }  
Run the preceding program and you get the results as in Figure 1.7.
 
clear arraylist 
Figure 1.7 Output for clear method

6. How to sort an ArrayList

You can sort any ArrayList in ascending order using the Sort() method. The Sort() method sorts the elements of an ArrayList. This method uses Array.Sort, that uses the QuickSort algorithm. Let’s see an example of sorting a person name list.

  1. using System;  
  2. using System.Collections;  
  3.   
  4. namespace ArrayListApplication  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             ArrayList personList = new ArrayList();  
  11.             personList.Add("Sandeep");  
  12.             personList.Add("Raviendra");  
  13.             personList.Add("Vinit");  
  14.             personList.Add("Lokesh");  
  15.             personList.Add("Prateek");  
  16.             Console.WriteLine("=========== Original List================");  
  17.             PrintValues(personList);  
  18.             //sort the list  
  19.             personList.Sort();  
  20.             Console.WriteLine("=========== Sorted List================");  
  21.             PrintValues(personList);  
  22.             Console.Read();  
  23.   
  24.         }  
  25.         private static void PrintValues(IEnumerable myList)  
  26.         {  
  27.             foreach (Object obj in myList)  
  28.             {  
  29.                 Console.WriteLine($"Name is: {obj}");  
  30.             }  
  31.         }  
  32.     }  
  33. }  
Execute this program and you get results as in Figure 1.8.
 
Sorted ArrayList 
Figure 1.8 Sorted ArrayList

7. Performance of ArrayList

There is a performance penalty in the use of ArrayList, particularly on value types. This is because boxing occurs, which is a way the runtime turns a value type into an object. Each element or item is added as an object to an ArrayList.