ArrayList in C#

The ArrayList class in C# is a dynamic, non-generic collection that provides the ability to store and manipulate objects of any type. It is part of the System.Collections namespace. The ArrayList is an alternative to using arrays when you need a collection that can dynamically grow or shrink in size.

To work with an ArrayList, you first need to import the System.Collections namespace:

using System.Collections;

Next, you can create an instance of the ArrayList class:

ArrayList myArrayList = new ArrayList();

The ArrayList is initially empty, but you can add items to it using the Add() method:

myArrayList.Add("Apple"); myArrayList.Add(42); myArrayList.Add(true);

In this example, we added a string, an integer, and a boolean value to the ArrayList. Note that ArrayList allows storing objects of different types since it is not type-safe like generic collections.

You can access the elements of the ArrayList using indexing:

string firstItem = (string)myArrayList[0]; int secondItem = (int)myArrayList[1]; bool thirdItem = (bool)myArrayList[2];

Since ArrayList stores objects as type 'object', you need to explicitly cast them to the appropriate type when retrieving them.

C# ArrayList | useful methods and properties

ArrayList provides several useful methods and properties. Let's explore a few of them:

Count: Returns the number of items in the ArrayList.
int count = myArrayList.Count;
Remove: Removes the specified item from the ArrayList.
Insert: Inserts an item at a specific index in the ArrayList.
myArrayList.Insert(1, "Orange");
Contains: Checks if the ArrayList contains a specific item.
bool containsApple = myArrayList.Contains("Apple");
Sort: Sorts the items in the ArrayList.

Following is an example program that demonstrates the basic usage of ArrayList in C#:

using System; using System.Collections; class Program { static void Main(string[] args) { // Create a new ArrayList ArrayList arList = new ArrayList(); // Add elements to the ArrayList arList.Add("red"); arList.Add("blue"); arList.Add("green"); arList.Add(100); arList.Add(3.142); // Access elements in the ArrayList Console.WriteLine("First element: " + arList[0]); // Output: "apple" // Iterate over the ArrayList Console.WriteLine("Iterating over the ArrayList:"); foreach (object item in arList) { Console.WriteLine(item); } // Insert an element into the ArrayList arList.Insert(3, "yellow"); Console.WriteLine("After inserting 'yellow':"); foreach (object item in arList) { Console.WriteLine(item); } // Remove an element from the ArrayList arList.Remove("green"); Console.WriteLine("After removing 'green':"); foreach (object item in arList) { Console.WriteLine(item); } // Search for an element in the ArrayList int index = arList.IndexOf("yellow"); Console.WriteLine("Index of 'yellow': " + index); } }
//Output: First element: red Iterating over the ArrayList: red blue green 100 3.142 After inserting 'yellow': red blue green yellow 100 3.142 After removing 'green': red blue yellow 100 3.142 Index of 'yellow': 2


Keep in mind that since ArrayList is not type-safe, you need to be cautious when retrieving items from it and ensure proper type casting. If you need type safety and better performance, consider using the generic List<T> class instead.