Back to: C#.NET Tutorials For Beginners and Professionals
ArrayList in C# with Examples
In this article, I am going to discuss the ArrayList in C# with Examples. Please read our previous article before proceeding to this article where we discussed the Introduction of Collection in C#. As part of this article, we are going to discuss the following important concepts of the ArrayList class.
- What is ArrayList in C#?
- Understanding the Methods and Properties of ArrayList class in C#.
- Examples of ArrayList class.
- What is the difference between an Array and ArrayList ?
What is ArrayList in C#?
The ArrayList in C# is a collection class that works like an array but provides the facilities such as dynamic resizing, adding and deleting elements from the middle of a collection. It implements the System.Collections.IList interface using an array whose size is dynamically increased as required.
Methods and Properties of ArrayList Collection class in C#:
The following are the methods and properties provided by the ArrayList collection class in C#.
- Add(object value): This method is used to add an object to the end of the collection.
- Remove(object obj): This method is used to remove the first occurrence of a specific object from the collection.
- RemoveAt(int index): This method takes the index position of the elements and remove that element from the collection.
- Insert(int index, Object value): This method is used to inserts an element into the collection at the specified index.
- Capacity: This property gives you the capacity of the collection means how many elements you can insert into the collection.
Example of ArrayList Collection class in C#:
Let us see an example by using the above methods and properties of the ArrayList class. The code is self-explained so please go through the comments.
using System; using System.Collections; namespace ArrayListCollection { class Program { static void Main(string[] args) { //Createing ArrayList collection using default constructor ArrayList al = new ArrayList(); Console.WriteLine("Initial Capacity: " + al.Capacity); al.Add(10); Console.WriteLine("Capacity after adding first item: " + al.Capacity); al.Add("hello"); al.Add(true); al.Add(3.14f); Console.WriteLine("Capacity after adding fourth item: " + al.Capacity); al.Add('A'); Console.WriteLine("Capacity after adding 5th element: " + al.Capacity); //Printing the ArrayList elements using for loop for (int i = 0; i < al.Count; i++) { Console.Write(al[i] + " "); } Console.WriteLine(); //Removing the values from the middle of the array list //here we are removing by value al.Remove(true); //You can also remove element by using index position // al.RemoveAt(2); //Printing the ArrayList elements using foreach loop after // removing an element from the collection foreach (object obj in al) { Console.Write(obj + " "); } Console.WriteLine(); //inserting values into the middle of the array list collection al.Insert(2, false); // Printing the values of the collection using foreach loop after // inserting an element into the middle of the collection foreach (object obj in al) { Console.Write(obj + " "); } Console.WriteLine(); // creating new ArrayList collection by passing the old // array list as parameter ArrayList coll = new ArrayList(al); Console.WriteLine("Initial capacity of new array list collection:" + coll.Capacity); // Printing the values of the new array list collection using foreach loop foreach (object obj in coll) { Console.Write(obj + " "); } Console.ReadKey(); } } }
OUTPUT:
What is the difference between an array and an Array List in C#?
This is one of the frequently asked interview questions in C#. So let us discuss the difference between an array and ArrayList.
Array:
- Fixed Length
- Cannot insert into the middle
- Cannot delete from middle
ArrayList:
- Variable Length
- Can insert into the middle of the collection
- Can insert into the middle of the collection
In the next article, I am going to discuss Hashtable in C# with some examples. Here, in this article, I try to explain ArrayList in C# with some examples. I hope this article will help you with your needs. I would like to have your feedback. Please post your feedback, question, or comments about this article.