C# ArrayList

C# ArrayList class is a collection class. In this article, learn how to work with ArrayList in C#.

Introduction

C# ArrayList is a collection class. This tutorial shows how to use ArrayList in a C# application including Adding ArrayList Items, Removing item from an ArrayList, ArrayList properties, and ArrayList methods. 

Create ArrayList

The following code craetes an ArrayList object in C#. 

  1. ArrayList arr = new ArrayList();

The datatype of an ArrayList is the object type. We can add any data types such as strings, integers to a collection.

Add ArrayList Items

The Add() method adds items to an ArrayList. The following code adds several items to an ArrayList. The items are of string type. The code also uses a foreach loop that reads all items of the ArrayList and displays on the console.

  1. namespace ArrayList1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             ArrayList arr = new ArrayList();  
  8.             arr.Add("Sunday");  
  9.             arr.Add("Monday");  
  10.             arr.Add("Tuesday");  
  11.             arr.Add("Wednesday");  
  12.             arr.Add("Thusday");  
  13.             arr.Add("Friday");  
  14.             arr.Add("Saturday");  
  15.             Console.WriteLine("The elements of the ArrayList are:");  
  16.             foreach (object obj in arr)  
  17.             {  
  18.                 Console.WriteLine(obj);  
  19.             }  
  20.         }  
  21.     }  
  22. } 

Here is the output.

arraylist_add.jpg

ArrayList Properties

Here is a list of ArrayList properties.

Capacity

Gets or sets the number of elements an ArrayList can store. The default capacity of an ArrayList is 4. If I add 5th element, then the capacity will be doubled (that is 8) relative to the previous one (that is 4). 

For example:

  1. namespace ArrayList1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             ArrayList arr = new ArrayList();  
  8.             arr.Add("Sunday");  
  9.             arr.Add("Monday");  
  10.             Console.WriteLine("**********Elements of ArrayList*************");  
  11.             Console.WriteLine();  
  12.             foreach (object obj in arr)  
  13.             {  
  14.                 Console.WriteLine(obj);  
  15.             }  
  16.             Console.WriteLine();  
  17.             Console.WriteLine("Capacity of the ArrayList is:" + arr.Capacity);  
  18.             Console.WriteLine();  
  19.             Console.WriteLine("//Adding Three more elements");  
  20.             Console.WriteLine("**********Elements of ArrayList*************");  
  21.             Console.WriteLine();  
  22.             arr.Add("Tuesday");  
  23.             arr.Add("Wednesday");  
  24.             arr.Add("Thusday");  
  25.             foreach (object obj in arr)  
  26.             {  
  27.                 Console.WriteLine(obj);  
  28.             }  
  29.             Console.WriteLine();  
  30.             Console.WriteLine("Now the Capacity is:" + arr.Capacity);  
  31.             Console.WriteLine();  
  32.             Console.WriteLine("//Adding Four more elements");  
  33.             Console.WriteLine("**********Elements of ArrayList*************");  
  34.             arr.Add("Friday");  
  35.             arr.Add("Saturday");  
  36.             arr.Add("Januray");  
  37.             arr.Add("Feburay");  
  38.             foreach (object obj in arr)  
  39.             {  
  40.                 Console.WriteLine(obj);  
  41.             }  
  42.             Console.WriteLine();  
  43.             Console.WriteLine("Now the Capacity is:" + arr.Capacity);  
  44.         }  
  45.     }  
  46. } 

Here is the output.

arraylist_capacity.jpg

Count

Gets the number of elements actually contained in the ArrayList. 

For example:

  1. namespace ArrayList1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             ArrayList arr = new ArrayList();  
  8.             arr.Add("Sunday");  
  9.             arr.Add("Monday");  
  10.             Console.WriteLine("**********Elements of ArrayList*************");  
  11.             Console.WriteLine();  
  12.             foreach (object obj in arr)  
  13.             {  
  14.                 Console.WriteLine(obj);  
  15.             }  
  16.             Console.WriteLine();  
  17.             Console.WriteLine("The number of element in the ArrayList are:" + arr.Count);  
  18.             Console.WriteLine();  
  19.             Console.WriteLine("//Adding Three more elements");  
  20.             Console.WriteLine("**********Elements of ArrayList*************");  
  21.             Console.WriteLine();  
  22.             arr.Add("Tuesday");  
  23.             arr.Add("Wednesday");  
  24.             arr.Add("Thusday");  
  25.             foreach (object obj in arr)  
  26.             {  
  27.                 Console.WriteLine(obj);  
  28.             }  
  29.             Console.WriteLine();  
  30.             Console.WriteLine("Now the number of elements in the ArrayList are:" + arr.Count);  
  31.             Console.WriteLine();  
  32.             Console.WriteLine("//Adding Four more elements");  
  33.             Console.WriteLine("**********Elements of ArrayList*************");  
  34.             arr.Add("Friday");  
  35.             arr.Add("Saturday");  
  36.             arr.Add("Januray");  
  37.             arr.Add("Feburay");  
  38.             foreach (object obj in arr)  
  39.             {  
  40.                 Console.WriteLine(obj);  
  41.             }  
  42.             Console.WriteLine();  
  43.             Console.WriteLine("Now the elements in the ArrayList are:" + arr.Count);  
  44.         }  
  45.     }  
  46. } 

Here is the output.

arraylist_count.jpg

ArrayList Methods


Clear() 

Removes all elements from an ArrayList. 

For example:

  1. namespace ArrayList1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             ArrayList arr = new ArrayList(7);  
  8.             arr.Add("Sunday");  
  9.             arr.Add("Monday");  
  10.             arr.Add("Tuesday");  
  11.             arr.Add("Wednesday");  
  12.             arr.Add("Thusday");  
  13.             arr.Add("Friday");  
  14.             arr.Add("Saturday");  
  15.             Console.WriteLine("The elements in the arraylist are:");  
  16.             foreach (object obj in arr)  
  17.             {  
  18.                 Console.WriteLine(obj);  
  19.             }  
  20.             Console.WriteLine("The number of element in the arraylist are:" + arr.Count);  
  21.             arr.Clear();  
  22.             Console.WriteLine("After apply the clear method the elements in the arraylist are:" + arr.Count);  
  23.         }  
  24.     }  
  25. } 

Here is the output.

arraylist_clear.jpg

Contains() 

Determines whether an element is in an ArrayList. 

For example:

  1. namespace ArrayList1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             ArrayList arr = new ArrayList(7);  
  8.             arr.Add("Sunday");  
  9.             arr.Add("Monday");  
  10.             arr.Add("Tuesday");  
  11.             arr.Add("Wednesday");  
  12.             arr.Add("Thusday");  
  13.             arr.Add("Friday");  
  14.             arr.Add("Saturday");  
  15.             Console.WriteLine("The elements in the arraylist are:");  
  16.             foreach (object obj in arr)  
  17.             {  
  18.                 Console.WriteLine(obj);  
  19.             }  
  20.             Console.WriteLine();  
  21.             Console.WriteLine("The element Saturday contain in the arraylist is:" + arr.Contains("Saturday"));  
  22.             Console.WriteLine("The element Monday contain in the arraylist is:" + arr.Contains("Monday"));  
  23.             Console.WriteLine("The element Tuesday contain in the arraylist is:" + arr.Contains("Tuesday"));  
  24.             Console.WriteLine("The element May contain in the arraylist is:" + arr.Contains("May"));  
  25.             Console.WriteLine("The element Hello contain in the arraylist is:" + arr.Contains("Hello"));  
  26.         }  
  27.     }  
  28. } 

Here is the output.

arraylist_contains.jpg

Insert()

Inserts an element into the ArrayList at the specified index.

For example:

  1. namespace ArrayList1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             ArrayList arr = new ArrayList(7);  
  8.             arr.Add("Sunday");  
  9.             arr.Add("Monday");  
  10.             arr.Add("Tuesday");  
  11.             arr.Add("Wednesday");  
  12.             arr.Add("Thusday");  
  13.             arr.Add("Friday");  
  14.             arr.Add("Saturday");  
  15.             Console.WriteLine("The elements in the arraylist are:");  
  16.             foreach (object obj in arr)  
  17.             {  
  18.                 Console.WriteLine(obj);  
  19.             }  
  20.             arr.Insert(0, "********Names of the days*******");  
  21.             arr.Insert(8, "*********Names of the months**********");  
  22.             arr.Insert(9, "Janurary");  
  23.             arr.Insert(10, "Feburary");  
  24.             arr.Insert(11, "March");  
  25.             arr.Insert(12, "April");  
  26.             arr.Insert(13, "May");  
  27.             Console.WriteLine("The elements in the arraylist are after the insert operation:");  
  28.             Console.WriteLine();  
  29.             foreach (object obj in arr)  
  30.             {  
  31.                 Console.WriteLine(obj);  
  32.             }  
  33.         }  
  34.     }  
  35. } 

Here is the output.

arraylist_insert.jpg

InsertRange()

Inserts the elements of a collection into the ArrayList at the specified index. 

For example:

  1. namespace ArrayList1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             ArrayList arr = new ArrayList(7);  
  8.             arr.Add("Sunday");  
  9.             arr.Add("Monday");  
  10.             arr.Add("Tuesday");  
  11.             arr.Add("Wednesday");  
  12.             arr.Add("Thusday");  
  13.             arr.Add("Friday");  
  14.             arr.Add("Saturday");  
  15.             Stack stack=new Stack();  
  16.             stack.Push("Janurary");  
  17.             stack.Push("Feburary");  
  18.             stack.Push("March");  
  19.             stack.Push("April");  
  20.             stack.Push("May");  
  21.             stack.Push("June");  
  22.             stack.Push("July");  
  23.             stack.Push("August");  
  24.             stack.Push("September");  
  25.             stack.Push("October");  
  26.             stack.Push("November");  
  27.             stack.Push("December");  
  28.             Console.WriteLine("The elements in the arraylist are:");  
  29.             foreach (object obj in arr)  
  30.             {  
  31.                 Console.WriteLine(obj);  
  32.             }  
  33.             arr.Insert(0, "********Names of the days*******");  
  34.             arr.Insert(8, "*********Names of the months**********");  
  35.             arr.InsertRange(9, stack);  
  36.             Console.WriteLine("The elements in the arraylist are after the insert operation:");  
  37.             Console.WriteLine();  
  38.             foreach (object obj in arr)  
  39.             {  
  40.                 Console.WriteLine(obj);  
  41.             }  
  42.         }  
  43.     }  
  44. } 

Here is the output.

arraylist_insertrange.jpg

IndexOf(object)

Searches for the specified object and returns the zero-based index of the first occurrence within the entire ArrayList.

For example:

  1. namespace ArrayList1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             ArrayList arr = new ArrayList(7);  
  8.             arr.Add("Sunday");  
  9.             arr.Add("Monday");  
  10.             arr.Add("Tuesday");  
  11.             arr.Add("Wednesday");  
  12.             arr.Add("Thusday");  
  13.             arr.Add("Friday");  
  14.             arr.Add("Saturday");  
  15.             Console.WriteLine("The elements in the arraylist are:");  
  16.             foreach (object obj in arr)  
  17.             {  
  18.                 Console.WriteLine(obj);  
  19.             }  
  20.             Console.WriteLine("The index value of Sunday is:" + arr.IndexOf("Sunday"));  
  21.             Console.WriteLine("The index value of Monday is:" + arr.IndexOf("Monday"));  
  22.             Console.WriteLine("The index value of Tuesday is:" + arr.IndexOf("Tuesday"));  
  23.             Console.WriteLine("The index value of Wednesday is:" + arr.IndexOf("Wednesday"));  
  24.             Console.WriteLine("The index value of Thusday is:" + arr.IndexOf("Thusday"));  
  25.             Console.WriteLine("The index value of Friday is:" + arr.IndexOf("Friday"));  
  26.             Console.WriteLine("The index value of Saturday is:" + arr.IndexOf("Saturday"));  
  27.         }  
  28.     }  
  29. } 

Here is the output.

arraylist_indexof.jpg

Remove()

Removes the first occurrence of a specific object from the ArrayList. 

For example:

  1. namespace ArrayList1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             ArrayList arr = new ArrayList(7);  
  8.             arr.Add("Sunday");  
  9.             arr.Add("Monday");  
  10.             arr.Add("Tuesday");  
  11.             arr.Add("Wednesday");  
  12.             arr.Add("Thusday");  
  13.             arr.Add("Friday");  
  14.             arr.Add("Saturday");  
  15.             Console.WriteLine("The elements in the arraylist are:");  
  16.             foreach (object obj in arr)  
  17.             {  
  18.                 Console.WriteLine(obj);  
  19.             }  
  20.             arr.Remove("Sunday");  
  21.             arr.Remove("Monday");  
  22.             arr.Remove("Tuesday");  
  23.             Console.WriteLine();  
  24.             Console.WriteLine("After remove the elements the arraylist is as:");  
  25.             foreach (object obj in arr)  
  26.             {  
  27.                 Console.WriteLine(obj);  
  28.             }  
  29.         }  
  30.     }  
  31. } 

Here is the output.

arraylist_remove.jpg

RemoveAt()

Removes the element at the specified index of the ArrayList. 

For example:

  1. namespace ArrayList1  
  2. {  
  3.   
  4.     class Program  
  5.     {  
  6.         static void Main(string[] args)  
  7.         {  
  8.             ArrayList arr = new ArrayList(7);  
  9.             arr.Add("Sunday");  
  10.             arr.Add("Monday");  
  11.             arr.Add("Tuesday");  
  12.             arr.Add("Wednesday");  
  13.             arr.Add("Thusday");  
  14.             arr.Add("Friday");  
  15.             arr.Add("Saturday");  
  16.             Console.WriteLine("The elements in the arraylist are:");  
  17.             foreach (object obj in arr)  
  18.             {  
  19.                 Console.WriteLine(obj);  
  20.             }  
  21.             arr.RemoveAt(3);  
  22.             arr.RemoveAt(1);  
  23.             arr.RemoveAt(2);  
  24.             Console.WriteLine();  
  25.             Console.WriteLine("After remove the elements the arraylist is as:");  
  26.             foreach (object obj in arr)  
  27.             {  
  28.                 Console.WriteLine(obj);  
  29.             }  
  30.         }  
  31.     }  
  32. } 

Here is the output.

arraylist_removeat.jpg

RemoveRange()

Removes a range of elements from the ArrayList.

For example:

  1. namespace ArrayList1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             ArrayList arr = new ArrayList(7);  
  8.             arr.Add("Sunday");  
  9.             arr.Add("Monday");  
  10.             arr.Add("Tuesday");  
  11.             arr.Add("Wednesday");  
  12.             arr.Add("Thusday");  
  13.             arr.Add("Friday");  
  14.             arr.Add("Saturday");  
  15.             Stack stack = new Stack();  
  16.             stack.Push("Janurary");  
  17.             stack.Push("Feburary");  
  18.             stack.Push("March");  
  19.             stack.Push("April");  
  20.             stack.Push("May");  
  21.             stack.Push("June");  
  22.             stack.Push("July");  
  23.             stack.Push("August");  
  24.             stack.Push("September");  
  25.             stack.Push("October");  
  26.             stack.Push("November");  
  27.             stack.Push("December");  
  28.             Console.WriteLine("The elements in the arraylist are:");  
  29.             foreach (object obj in arr)  
  30.             {  
  31.                 Console.WriteLine(obj);  
  32.             }  
  33.             arr.Insert(0, "********Names of the days*******");  
  34.             arr.Insert(8, "*********Names of the months**********");  
  35.             arr.InsertRange(9, stack);  
  36.             Console.WriteLine("The elements in the arraylist are after the insert operation:");  
  37.             Console.WriteLine();  
  38.             foreach (object obj in arr)  
  39.             {  
  40.                 Console.WriteLine(obj);  
  41.             }  
  42.             arr.RemoveRange(9, 12);  
  43.             Console.WriteLine();  
  44.             Console.WriteLine("After apply the RemoveRange operation the elements are:");  
  45.             Console.WriteLine();  
  46.             foreach (object obj in arr)  
  47.             {  
  48.                 Console.WriteLine(obj);  
  49.             }  
  50.         }  
  51.     }  
  52. } 

Here is the output.

arraylist_removerange.jpg

Reverse()

Reverses the order of the elements in the entire ArrayList.

For example:

  1. namespace ArrayList1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             ArrayList arr = new ArrayList();  
  8.             arr.Add(1);  
  9.             arr.Add(23);  
  10.             arr.Add(12);  
  11.             arr.Add(11);  
  12.             arr.Add(45);  
  13.             arr.Add(20);  
  14.             arr.Add(54);  
  15.             Console.WriteLine("The elements in the arraylist are:");  
  16.             foreach (object obj in arr)  
  17.             {  
  18.                 Console.WriteLine(obj);  
  19.             }  
  20.             arr.Reverse();  
  21.             Console.WriteLine("After apply reverse methods the elements are as:");  
  22.             foreach (object obj in arr)  
  23.             {  
  24.                 Console.WriteLine(obj);  
  25.             }  
  26.         }  
  27.     }  
  28. } 

 

Here is the output.

arraylist_reverse.jpg

Sort()

Sorts the elements in the entire ArrayList.

For example:

  1. namespace ArrayList1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             ArrayList arr = new ArrayList();  
  8.             arr.Add(1);  
  9.             arr.Add(23);  
  10.             arr.Add(12);  
  11.             arr.Add(11);  
  12.             arr.Add(45);  
  13.             arr.Add(20);  
  14.             arr.Add(54);  
  15.             Console.WriteLine("The elements in the arraylist are:");  
  16.             foreach (object obj in arr)  
  17.             {  
  18.                 Console.WriteLine(obj);  
  19.             }  
  20.             arr.Sort();  
  21.             Console.WriteLine("After apply sort methods the elements are as:");  
  22.             foreach (object obj in arr)  
  23.             {  
  24.                 Console.WriteLine(obj);  
  25.             }  
  26.         }  
  27.     }  
  28. } 

Here is the output.

arraylist_sort.jpg

BinarySearch(object) 

Searches the entire sorted ArrayList for an element using the default comparer and returns the zero-based index of the element.  

For example:

  1. namespace ArrayList1  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             ArrayList arr = new ArrayList();  
  8.             arr.Add(1);  
  9.             arr.Add(23);  
  10.             arr.Add(12);  
  11.             arr.Add(11);  
  12.             arr.Add(45);  
  13.             arr.Add(20);  
  14.             arr.Add(54);  
  15.             Console.WriteLine("The elements in the arraylist are:");  
  16.             foreach (object obj in arr)  
  17.             {  
  18.                 Console.WriteLine(obj);  
  19.             }  
  20.             arr.Sort();  
  21.             Console.WriteLine("The sorted list is as:");  
  22.             foreach (object obj in arr)  
  23.             {  
  24.                 Console.WriteLine(obj);  
  25.             }  
  26.             Console.WriteLine("The element 23 searched at:"+arr.BinarySearch(23));  
  27.             Console.WriteLine("The element 12 searched at:" + arr.BinarySearch(12));                        
  28.         }  
  29.     }  
  30. } 

Here is the output.

arraylist_binarysearch.jpg  

Summary

In this article, I've explained an ArrayList class and its methods and properties and how to use them in a C# application.

 
Next : C# SortedList