---
title: "ArrayList Class in C#"  
description: "The ArrayList class can store heterogeneous collections of objects unlike array which store homogeneous collection of data."  
author: "Anupam Mishra"  
published: 2015-12-30  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/11973/arraylist-class-in-c-sharp  
category: "c#"  
tags: ["c#", "collection"]  
reading_time: 4 minutes  

---

# ArrayList Class in C#

##### Introduction:

The ArrayList class can store heterogeneous collections of objects unlike array which store homogeneous [collection](https://www.mindstick.com/articles/1718/collections-in-java) of data. Basically, ArrayList class is an [alternative](https://www.mindstick.com/forum/2324/what-is-the-alternative-to-the-mvc) of array. Using ArrayList you can add and remove items from a list at specified index. ArrayList also allows dynamic [memory allocation](https://www.mindstick.com/forum/157955/what-is-dynamic-memory-allocation-in-c-and-how-is-it-used-to-allocate-memory-at-runtime) for adding, searching and sorting items in the list.

Multidimensional arrays is not supported by the ArrayList. As elements are added to an ArrayList, the capacity is automatically increased as [required](https://yourviews.mindstick.com/view/81031/vigilence-required-during-corona-pandemic) through reallocation. You can decreases your ArrayList size by calling TrimToSize or by setting the Capacity [property](https://www.mindstick.com/articles/198559/an-ownership-of-property-in-hua-hin) explicitly.

The ArrayList collection accepts null as valid value. It also allows [duplicate](https://www.mindstick.com/forum/160730/contain-duplicate) item.

##### ArrayList Constructors:

1. ArrayList (): Initializes a new [instance](https://www.mindstick.com/forum/155658/testing-connection-to-cloud-sql-instance-with-a-mysql-client-from-vm-instance) of the ArrayList class that is empty and has the default initial capacity.

2. ArrayList (ICollection): Initializes a new instance of the ArrayList class that contains elements copied from the specified collection and that has the same initial capacity as the number of elements copied.

3. ArrayList (Int32): Initializes a new instance of the empty ArrayList class with initial capacity passed as parameter.

For creation of ArrayList using its [default constructor](https://www.mindstick.com/forum/34531/default-constructor), code is as follows:

```
ArrayList arrayList = new ArrayList();// adding elements in the ArrayListarrayList.Add(18); // you can define any type of objects such as Integer, String etc.// retrieving elementsforeach ( var p in arrayList){    Console.WriteLine(p+” ”);}
```

## \

##### Properties and Methods of ArrayList:

##### Some important Properties and Methods of ArrayList are:

1. Capacity: For checking the capacity of ArrayList object. By default, ArrayList capacity is 4. If you add 5th element in the list then its capacity will be incremented by its default capacity, this case its capacity will become 8. Its capacity will remain unchanged till we have less than 9 elements but as soon as we add its capacity will automatically be increased by default capacity i.e. 4 and its new capacity will be 12.

We can get current capacity of arraylist using “Capacity” property

Example: arrayList.Capacity

2. Sort: For sorting a ArrayList, we use sort() method:

Example: arrayList.Sort();

3. Reverse: For reversing a ArrayList, we use reverse() method.

Example: arrayList.Reverse();

4. Remove: For removing all elements in ArrayList, we use remove() method.

Example: arrayList.Remove();

We can also remove single element at a time by using RemoveAt(int index) method.

Example: arrayList.RemoveAt(3);

5. IndexOf(object): For finding index number of specified object, we use IndexOf(object) method.

Example: arrayList.IndexOf(25);

Similarly, there are other helpful [properties](https://yourviews.mindstick.com/view/81845/now-it-s-possible-to-predict-properties-of-any-molecule) that are defined in ArrayList class which we can use. For complete list of properties and methods visit following link:

[https://msdn.microsoft.com/en-us/library/system.collections.arraylist(v=vs.110).aspx](https://msdn.microsoft.com/en-us/library/system.collections.arraylist(v=vs.110).aspx)

##### Below is an example to demonstrate the use of ArrayList.

```
using System;using System.Collections; namespace DemoArrayList{class Program  {      static void Main(string[] args)    {            Console.WriteLine(@"## Lets us try to use ArrayList class in a program");            Console.WriteLine();            ArrayList arrayList = new ArrayList();            Console.WriteLine("Initial Capacity: " + arrayList.Capacity);            Console.WriteLine();            Console.WriteLine("Enter the five element in ArrayList: ");            Console.WriteLine();            for (int i = 0; i < 5; i++)            {                arrayList.Add(Console.ReadLine());            }            Console.WriteLine();            Console.WriteLine(@"## Retrieving data from the list:");            //Console.WriteLine();            //Console.WriteLine();            foreach (var p in al)            {                Console.Write(p.ToString() + " ");            }            Console.WriteLine();            Console.WriteLine("After adding 5 elements capacity is: "+ arrayList.Capacity);            Console.WriteLine();            arrayList.Reverse();            Console.WriteLine();            Console.WriteLine(@"## Reversing item to the list: ");            Console.WriteLine();            foreach (var p in arrayList)            {                Console.Write(p.ToString() + " ");            }            Console.ReadKey();}   }}
```

## Output:

![ArrayList Class in C#](https://www.mindstick.com/mindstickarticle/055d526d-6f60-4c15-ae5c-4378a10df1d6/images/c15cf9b1-f77b-4daa-903f-6dc91c53c368.png)

##### Advantage of ArrayList:

##### · The ArrayList stores everything as object so we can store almost any value

##### regardless of their data types in a single list unlike array.

##### · No need to do explicitly allocation or deallocation for storing a data.

##### · You can insert and delete the elements between positions in the ArrayList.

\

---

Original Source: https://www.mindstick.com/articles/11973/arraylist-class-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
