In c#, ArrayList is useful to store elements of different data types. The size of ArrayList can grow or shrink dynamically based on the need of our application, like adding or removing elements from ArrayList.
In c#, arraylists are same as arrays, but the only difference is that arrays are used to store a fixed number of the same data type elements.
In c#, ArrayList is a non-generic type of collection, and it is provided by System.Collections namespace.
As discussed, the collection is a class, so to define an arraylist, you need to declare an instance of the arraylist class before we perform any operations like add, delete, etc. as shown below.
If you observe the above arraylist declaration, we created a new arraylist (arrList) with an instance of arraylist class without specifying any size.
The following are some of the commonly used properties of an arraylist in the c# programming language.
Property | Description |
---|---|
Capacity | It is useful to get or set the number of elements an arraylist can contain. |
Count | It is useful to get the number of elements in an arraylist. |
IsFixedSize | It is useful to get a value to indicate whether the arraylist has a fixed size or not. |
IsReadOnly | It is useful to get a value to indicate whether the arraylist is read-only or not. |
Item | It is used to get or set an element at the specified index. |
IsSynchronized | It is used to get a value to indicate whether access to arraylist is synchronized (thread-safe) or not. |
The following are some of the commonly used methods of an arraylist to add, search, insert, delete, or sort elements of arraylist in c# programming language.
Method | Description |
---|---|
Add | It is useful to add an element at the end of the arraylist. |
AddRange | It is useful to add all the elements of the specified collection at the end of the arraylist. |
Clear | It will remove all the elements in the arraylist. |
Clone | It will create a shallow copy of the arraylist. |
Contains | It is useful to determine whether the specified element exists in an arraylist or not. |
CopyTo | It is useful to copy all the elements of an arraylist into another compatible array. |
GetRange | It is useful to return a specified range of arraylist elements starting from the specified index. |
IndexOf | It is useful to search for a specified element and return a zero-based index if found; otherwise, return -1 if no element is found. |
Insert | It is useful to insert an element in the arraylist at the specified index. |
InsertRange | It is useful to insert all the specified collection elements into an arraylist starting from the specified index. |
Remove | It is useful to remove the first occurrence of a specified element from the arraylist. |
RemoveAt | It is useful to remove an element from the arraylist based on the specified index position. |
RemoveRange | It is useful to remove a range of elements from an arraylist. |
Reverse | It reverses the order of arraylist elements. |
Sort | It sorts the elements in an arraylist. |
ToArray | It copies all the elements of an arraylist to a new array object. |
Following is the example of using an arraylist in the c# programming language.
If you observe the above example, we are able to define a new arraylist (arrlist) collection by using the System.Collections namespace. Here, we are binding different data type values to the newly created arraylist (arrlist) by using Add method. With Count & Capacity properties, we can access a number of elements in an arraylist.
When you execute the above c# program, you will get the result below.
This is how you can use arraylist in c# to hold different data type objects based on our requirements.
The following are useful examples to work with arraylist in c# programming language.
In c#, by using the Add or AddRange method, we can add different data type elements to an arraylist.
Following is the example of adding elements to arraylist using Add or AddRange methods in c#.
If you observe the above example, we created multiple arraylists (arrlist, arrlist2) and added elements to the arraylists in different ways.
When you execute the above c# program, you will get the result below.
In c#, you have different ways to access ArrayList elements, i.e., either using index positions or by iterating through an ArrayList using for / foreach loops.
Following is an example of accessing arraylist elements in different ways.
If you observe the above example, we are accessing arraylist elements in different ways by using index positions for and foreach loops based on our requirements.
When you execute the above c# program, you will get the result below.
In c#, we can insert elements into the arraylist either using Insert() or InsertRange() methods.
Following is the example of inserting elements into an arraylist by using Insert and InsertRange methods in c#.
If you observe the above example, we inserted elements into the arraylist at different index positions (0, 1) by using the Insert() method. Similarly, using the InsertRange() method we are inserting all the elements of the newly created arraylist (arrlist2) into the arrlist at index position 2.
When you execute the above c# program, you will get the result below.
In c#, you have different methods to remove elements from the arraylist i.e. either by using Remove() or RemoveAt(), or RemoveRange() methods.
Following is the example of deleting elements from arraylist in c# programming language.
If you observe the above example, we used a Remove() method to delete a particular value of an element from arraylist and used a RemoveAt() method to delete an element at a specified index position.
Same way, we used the RemoveRange() method to delete the specified number of elements from the arraylist starting from the specified index position.
When you execute the above c# program, you will get the result below.
If you want to remove all the elements from the arraylist, use the Clear() method.
Same way, we can use different methods like the Sort() method to sort elements of the arraylist and the Reverse() method to reverse the order of arraylist elements.
By using Contains() method, we can check whether the specified element exists in the arraylist or not. In case it exists, it will return true otherwise false.
Following is the example of using Contains() method to check for an item that exists in arraylist or not in c#.
If you observe the above example, we used a Contains() method to check for an item (100) exists in arraylist (arrlist) or not.
When you execute the above c# program, you will get the result below.
The following are the important points that need to remember about arraylist in c#.
The following are the main difference between array and arraylist in the c# programming language.