ArrayList in C#
ArrayList is a powerful feature of C# language. It is the non-generic type of collection which is defined in System.Collections namespace. It is used to create a dynamic array means the size of the array is increase or decrease automatically according to the requirement of your program, there is no need to specify the size of the ArrayList. Or in other words, ArrayList represents an ordered collection of an object that can be indexed individually. In ArrayList, you can store elements of the same type and of the different types. It belongs to the non-generic collection. The below diagram illustrates the ArrayList class hierarchy:
- The ArrayList class implements the IEnumerable, ICollection, IList, and ICloneable interfaces.
- The ArrayList class inherits the Object class.
- The ArrayList is defined under System.Collections namespace. So, when you use Arraylist in your program you must add System.Collections namespace.
- You cannot implement a multi-dimensional array using ArrayList.
- The capacity of an ArrayList is the number of elements the ArrayList can hold.
- You are allowed to use duplicate elements in your ArrayList.
- You can apply searching and sorting on the elements present in the ArrayList.
- Arraylist can accept null as a valid value.
How to create the ArrayList?
ArrayList class has three constructors which are used to create the ArrayList.
- ArrayList(): This constructor is used to create an instance of ArrayList class which is empty and having no initial capacity.
- ArrayList(Int32): This constructor is used to create an instance of ArrayList class which is empty and having the specified initial capacity.
- ArrayList(ICollection): This constructor is used to create an array list initialized with the elements from the specified collection and having the same initial capacity which is copied from collection.
Let’s see how to create an ArrayList using ArrayList() constructor:
- Step 1: Include System.Collections namespace in your program with the help of using keyword.
- Step 2: Create an ArrayList using ArrayList class as shown below:
ArrayList list_name = new ArrayList();
- Step 3: If you want to add elements in your ArrayList then use Add() method to add elements in your ArrayList. As shown in the below example.
- Step 4: The elements of the ArrayList is accessed by using a foreach loop, or by for loop, or by indexer. As shown in the below example we access the ArrayList using a foreach loop.
Example: Below program show how to create an ArrayList, adding elements to the ArrayList, and how to access the elements of the ArrayList.
12.56 GeeksforGeeks G 1234
How to find the Capacity and Count of elements of the ArrayList?
Number of elements: 5 Current capacity: 8
How to remove elements from the ArrayList?
In ArrayList, you are allowed to remove elements from the ArrayList. ArrayList provides four different methods to remove elements and the methods are:
- Remove: This method is used to remove the first occurrence of a specific object from the ArrayList.
- RemoveAt: This method is used to remove the element at the specified index of the ArrayList.
- RemoveRange: This method is used to remove a range of elements from the ArrayList.
- Clear: This method is used to remove all the elements from the ArrayList.
Initial number of elements : 13 After Remove() method the number of elements: 12 After RemoveAt() method the number of elements: 11 After RemoveRange() method the number of elements: 8 After Clear() method the number of elements: 0
How to sort the elements of the ArrayList?
In ArrayList, you can perform sorting on the elements present in the given ArrayList using the Sort() method. This method uses the QuickSort algorithm to perform sorting on the ArrayList and the elements are arranged in ascending order. The use of this method is shown in the below example.
ArrayList before using Sort() method: 1 6 40 10 5 3 2 4 ArrayList after using Sort() method: 1 2 3 4 5 6 10 40
- You can also create the object of ArrayList class using the IEnumerable, ICollection, and IList interfaces but some methods of ArrayList class will not be applicable on them as they are not the members of these interfaces.
// using IList Interface IList arrlist1 = new ArrayList(); // using ICollection Interface ICollection arrlist2 = new ArrayList(); // using IEnumerable Interface IEnumerable arrlist3 = new ArrayList();
But here, you cannot apply the methods like Reverse(), AddRange() etc.
- To read more about the constructors, properties, and methods of ArrayList class please refer to ArrayList Class.
- To read about the difference between Array and ArrayList please refer Array vs ArrayList in C#.