ArrayList in C# is a dynamically sized collection class provided by .NET framework, it allows to store and manage a list of objects of any data type.
Introduction to ArrayList in C#:
ArrayList is part of the System.Collections namespace, it is a non-generic collection that allows you to store a collection of objects that can grow or shrink dynamically in size.
Key features of ArrayList in C#:
-
- ArrayLists can dynamically adjust their size as you add or remove elements from the collection.
- It can hold objects of different data types within the same collection, offering flexibility for handling different types of data.
- It uses zero-based indexing while accessing the elements, just like Arrays.
- Items in the ArrayList are typically objects, so it may require boxing and unboxing while accessing elements in it.
- ArrayLists are useful when you need to handle different data types in the same collection.
Creating and Manipulating ArrayLists in C#:
To create an ArrayList, you need to include the System.Collections namespace and then instantiate an instance of the ArrayList class.
1 2 3 4 5 6 7 8 9 10 11 |
using System; using System.Collections; class Program { static void Main() { // Create an ArrayList object ArrayList tempList = new ArrayList(); } } |
Adding and Removing Elements:
You can use the Add
method to add elements to an ArrayList and use the Remove
or RemoveAt
method to remove elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System.Collections; namespace ArrayListCollectionSample { class Program { static void Main(string[] args) { //Creation of Arralist object ArrayList tempList = new ArrayList(); //Adding elements to tempList tempList.Add(1500); //Adding an integer to the templist tempList.Add("Mango"); //Adding a string to the templist tempList.Add(true);//Adding a boolean to the templist // Removing elements tempList.Remove("Mango"); // Removes the first occurrence of "Mango" tempList.RemoveAt(1); // Removes the element at index 1 } } } |
In the above example,
-
- We have created a ArrayList object as
tempList
and added a string (“Mango“) , integer (1500) and boolean (true) to it. tempList.Remove("Mango")
removes “Mango” andtempList.RemoveAt(1)
removes boolean value true from thetempList
.
- We have created a ArrayList object as
Accessing ArrayList Elements:
ArrayList uses zero-based indexing while accessing the elements, just like arrays.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.Collections; class Program { static void Main() { ArrayList myList = new ArrayList(); myList.Add("Apple"); myList.Add("Banana"); myList.Add("Orange"); // Accessing elements string firstFruit = (string)myList[0]; string secondFruit = (string)myList[1]; } } |
In this example,
-
- We create an ArrayList called
myList
and add elements to it, using the zero-based index we access items from it.
- We create an ArrayList called
Example 1: Create and use an ArrayList in C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
using System; using System.Collections; class Program { static void Main() { // Create an ArrayList ArrayList myList = new ArrayList(); // Add elements to the ArrayList myList.Add("Apple"); myList.Add(42); myList.Add(true); // Access elements Console.WriteLine(myList[0]); // Output: Apple Console.WriteLine(myList[1]); // Output: 42 Console.WriteLine(myList[2]); // Output: True // Remove an element myList.Remove("Apple"); // Count of elements Console.WriteLine("Number of elements: " + myList.Count); // Output: Number of elements: 2 } } |
In this example,
-
- We create an ArrayList called
myList
and add elements of different data types, e.g Apple (String type), 42 (Interger type) and true (boolean type) tomyList
. - We have used zero-based indexing and able to access the values and print in the console.
- We have also removed an item using the Remove method and display the count of elements in the ArrayList by using Count method.
- We create an ArrayList called
When we run above example, it generates following output.

ArrayList Methods and Properties in C#:
ArrayLists offer various methods and properties for manipulation and information retrieval.
- Count : We use the
Count
property to determine the number of elements. - Sort : The
Sort
method arranges the elements in ascending order. - Contains : The
Contains
method checks if the ArrayList contains a specific element. - Reverse : The
Reverse
method reverses the order of elements.
Example 2 : Showcase various ArrayList methods and properties in C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
using System; using System.Collections; namespace ArrayListCollectionSample { class Program { static void Main(string[] args) { ArrayList tempList = new ArrayList(); // Adding elements tempList.Add("Grapes"); tempList.Add("Orange"); tempList.Add("Mango"); tempList.Add("Banana"); tempList.Add("Watermelon"); // Count property int itemCount = tempList.Count; // Returns the number of elements in the ArrayList Console.WriteLine($"Number of elements in the tempList : {itemCount}"); // Sorting the elements tempList.Sort(); // Sorts the elements in ascending order // Displaying the modified ArrayList Console.WriteLine("Modified ArrayList after tempList.Sort() execution:"); Console.WriteLine("**********************************************************"); foreach (object item in tempList) { Console.WriteLine(item); } Console.WriteLine("**********************************************************"); // Checking if an element exists bool hasApple = tempList.Contains("Orange"); Console.WriteLine($"tempList has Orange ? : {hasApple}"); // Accessing elements string secondElement = (string)tempList[1]; Console.WriteLine($"Second element in tempList: {secondElement}"); // Removing an element tempList.Remove("Banana"); // Removes "Banana" Console.WriteLine("Removes Banana from tempList using tempList.Remove()."); // Reversing the elements tempList.Reverse(); // Reverses the order of elements // Displaying the modified ArrayList Console.WriteLine("Modified ArrayList after tempList.Reverse() execution:"); Console.WriteLine("**********************************************************"); foreach (object item in tempList) { Console.WriteLine(item); } Console.WriteLine("**********************************************************"); } } } |
In this example,
-
- We create an ArrayList called
myList
and perform various operations using its methods and properties.
- We create an ArrayList called
When we run the above program, it produces below output.

Difference between Array and Arraylist in C#:
Arrays |
ArrayLists |
---|---|
We have to specify the Array size during declaration. | ArrayList is dynamically resizable, and no size specification required. |
Array holds elements of a specific data type. | ArrayList store elements of various data types. |
We can’t change array size after declaration. | It grows dynamically or shrinks as elements are added or removed. |
Generally better performance due to fixed size and direct memory access. | Potentially slower performance due to dynamic resizing and value type boxing/unboxing. |
Array is typically Type-safe, elements must have the same data type. | ArrayList is not Type-safe, can hold different data types. |
Memory allocated in a contiguous block, may lead to memory wastage. | Internally manages memory allocation and resizing. |
No type conversion required for element access. | Accessing an element may requires type conversion (boxing/unboxing). |
Suitable for fixed-size collections with the same data type. | Useful for dynamic collections with varying data types. |
ArrayList or List<T> in C#? Which one is better?
Let’s compare between non generic ArrayList and generic collection List<T> and understand which one is better.
-
- List<T> is a strong typed, it ensures that all elements within the collection are of the specified type.
- ArrayList stores elements as objects, which requires boxing (converting value types to object types) and unboxing (converting back to value types). This boxing and unboxing can lead to performance overhead and memory allocation. List<T> is strongly typed and hence avoids boxing and unboxing, which results in better performance.
- Compile time type checking is feasible in List<T>, making it easier to catch type-related errors during development. This leads to more reliable and maintainable code.
- List<T> is strongly typed which is more memory efficient, whereas ArrayList requires additional memory for boxing value types and storing objects.
Conclusion:
In conclusion, if you’re working with collections of the same data type, List<T>
is the recommended choice due to its advantages, ArrayList
might still be used in legacy codebases or in scenarios where you are handling different data type collection.
Thanks for visiting, please go through my other articles on similar topics List in C# and Dictionary in C# – Collection Class