C# Tutorials

C# ArrayList:

ArrayList is a non-generic type of collection in C#. It can contain elements of any data types. It is similar to an array, except that it grows automatically as you add items in it. Unlike an array, you don't need to specify the size of ArrayList.

Example: Initialize ArrayList

ArrayList myArryList = new ArrayList();

Important Properties and methods of ArrayList:

Property Description
Capacity Gets or sets the number of elements that the ArrayList can contain.
Count Gets the number of elements actually contained in the ArrayList.
IsFixedSize Gets a value indicating whether the ArrayList has a fixed size.
IsReadOnly Gets a value indicating whether the ArrayList is read-only.
Item Gets or sets the element at the specified index.
Method Description
Add()/AddRange() Add() method adds single elements at the end of ArrayList.
AddRange() method adds all the elements from the specified collection into ArrayList.
Insert()/InsertRange() Insert() method insert a single elements at the specified index in ArrayList.
InsertRange() method insert all the elements of the specified collection starting from specified index in ArrayList.
Remove()/RemoveRange() Remove() method removes the specified element from the ArrayList.
RemoveRange() method removes a range of elements from the ArrayList.
RemoveAt() Removes the element at the specified index from the ArrayList.
Sort() Sorts entire elements of the ArrayList.
Reverse() Reverses the order of the elements in the entire ArrayList.
Contains Checks whether specified element exists in the ArrayList or not. Returns true if exists otherwise false.
Clear Removes all the elements in ArrayList.
CopyTo Copies all the elements or range of elements to compitible Array.
GetRange Returns specified number of elements from specified index from ArrayList.
IndexOf Search specified element and returns zero based index if found. Returns -1 if element not found.
ToArray Returns compitible array from an ArrayList.

Add elements into ArrayList:

The AddRange() method can take any type of collection that implements the ICollection interface e.g. List, ArrayList, SortedList, Queue, Stack, HashSet, Hashtable, etc.

Use the Add()method to add a single element or the AddRange() method to add multiple elements from the other collections into an ArrayList. Here, the element means the literal value of a primitive or non-primitive type.

Add() signature: int Add(Object value)

AddRange() signature:void AddRange(ICollection c)

Example: Add elements into ArrayList

ArrayList arryList1 = new ArrayList();
arryList1.Add(1);
arryList1.Add("Two");
arryList1.Add(3);
arryList1.Add(4.5);

ArrayList arryList2 = new ArrayList();
arryList2.Add(100);
arryList2.Add(200);

//adding entire arryList2 into arryList1
arryList1.AddRange(arryList2);

You can also add items when you initialize it using object initializer syntax.


ArrayList arrayList = new ArrayList() { 100, "Two", 12.5, 200 };

Access ArrayList Elements:

ArrayList elements can be accessed using indexer, in the same way as an array. However, you need to cast it to the appropriate type or use the implicit type var keyword while accessing it.

Example: Access individual element

ArrayList myArryList = new ArrayList();
myArryList.Add(1);
myArryList.Add("Two");
myArryList.Add(3);
myArryList.Add(4.5f);

//Access individual item using indexer
int firstElement = (int) myArryList[0]; //returns 1
string secondElement = (string) myArryList[1]; //returns "Two"
int thirdElement = (int) myArryList[2]; //returns 3
float fourthElement = (float) myArryList[3]; //returns 4.5

//use var keyword
var firstElement = myArryList[0]; //returns 1

Use a foreach or a for loop to iterate an ArrayList.

Example: Iterate ArrayList

ArrayList myArryList = new ArrayList();
myArryList.Add(1);
myArryList.Add("Two");
myArryList.Add(3);
myArryList.Add(4.5);

foreach (var val in myArryList)
                Console.WriteLine(val); 
            
//Or
for(int i = 0 ; i< myArryList.Count; i++)
                Console.WriteLine(myArryList[i]);

Output:
1
Two
3
4.5
Note : An ArrayList can contain multiple null and duplicate values.

Insert elements into ArrayList:

Use the Insert() method to insert a single item at the specified index.

Insert() signature: void Insert(int index, Object value)

Example: Insert()

ArrayList myArryList = new ArrayList();
myArryList.Add(1);
myArryList.Add("Two");
myArryList.Add(3);
myArryList.Add(4.5);

myArryList.Insert(1, "Second Item");
myArryList.Insert(2, 100);

foreach (var val in myArryList)
                Console.WriteLine(val); 

Output:
1
Second Item
100
Two
3
4.5

Use the InsertRange() method to insert all the values from another collection into ArrayList at the specfied index.

InsertRange() method signature: Void InsertRange(int index, ICollection c)

Example: InsertRange()

ArrayList arryList1 = new ArrayList();
arryList1.Add(100);
arryList1.Add(200);

ArrayList arryList2 = new ArrayList();
arryList2.Add(10);
arryList2.Add(20);
arryList2.Add(30);

arryList2.InsertRange(2, arryList1);

foreach(var item in arryList2)
                Console.WriteLine(item);

Output:
10
20
100
200
30

Remove elements from an ArrayList:

Use the Remove() method to remove a specified element from an ArrayList.

Remove() signature: void Remove(Object obj)

Example: Remove()

ArrayList arryList1 = new ArrayList();
arryList1.Add(100);
arryList1.Add(200);
arryList1.Add(300);

arryList1.Remove(100); //Removes 1 from ArrayList

foreach (var item in arryList1)
                Console.WriteLine(item);

Output:
200
300

Use the RemoveAt() method to remove an element from the specified index location.

RemoveAt() method signature: void RemoveAt(int index)

Example: RemoveAt()

ArrayList arryList1 = new ArrayList();
arryList1.Add(100);
arryList1.Add(200);
arryList1.Add(300);

arryList1.RemoveAt(1); //Removes the first element from an ArrayList

foreach (var item in arryList1)
                Console.WriteLine(item);

Output:
100
300

Use the RemoveRange() method to remove multiple elements from the specified index till the specified number of elements in the ArrayList.

RemoveRange() signature: void RemoveRange(int index, int count)

Example: RemoveRange()

ArrayList arryList1 = new ArrayList();
arryList1.Add(100);
arryList1.Add(200);
arryList1.Add(300);

arryList1.RemoveRange(0,2);//Removes two elements starting from 1st item (0 index)

foreach(var item in arryList1)
                Console.WriteLine(item);

Output:
300

Sort ArrayList:

ArrayList includes Sort() and Reverse() method. Sort() method arranges elements in ascending order. However, all the elements should have same data type so that it can compare with default comparer otherwise it will throw runtime exception.

Reverse() method arranges elements in reverse order. Last element at zero index and so on.

Example: Sort(), Reverse()

ArrayList arryList1 = new ArrayList();
arryList1.Add(300);
arryList1.Add(200);
arryList1.Add(100);
arryList1.Add(500);
arryList1.Add(400);

Console.WriteLine("Original Order:");

foreach(var item in arryList1)
                Console.WriteLine(item);

arryList1.Reverse();
Console.WriteLine("Reverse Order:");

foreach(var item in arryList1)
                Console.WriteLine(item);

arryList1.Sort();
Console.WriteLine("Ascending Order:");

foreach(var item in arryList1)
                Console.WriteLine(item);

Output:
Original Order:
300
200
100
500
400
Reverse Order:
400
500
100
200
300
Ascending Order:
100
200
300
400
500

Check existing elements:

ArrayList.Contains() method checks whether specified element exists in the ArrayList or not. Returns true if exists otherwise false.

Example: Contains()

ArrayList myArryList = new ArrayList();
myArryList.Add(100);
myArryList.Add("Hello World");
myArryList.Add(300);

Console.WriteLine(myArryList.Contains(100)); 

Output:
True

Further Reading:

Points to Remember :

  1. ArrayList can store items(elements) of any datatype.
  2. ArrayList resizes automatically as you add the elements.
  3. ArrayList values must be cast to appropriate data types before using it.
  4. ArrayList can contain multiple null and dulplicate items.
  5. ArrayList can be accessed using foreach or for loop or indexer.
  6. Use Add(), AddRange(), Remove(), RemoveRange(), Insert(), InsertRange(), Sort(), Reverse() methods.

Learn about SortedList next.

;