ArrayList in C#

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.

  1. What is ArrayList in C#?
  2. Understanding the Methods and Properties of ArrayList class in C#.
  3. Examples of ArrayList class.
  4. What is the difference between an Array and ArrayList in C#?
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#.

  1. Add(object value): This method is used to add an object to the end of the collection.
  2. Remove(object obj): This method is used to remove the first occurrence of a specific object from the collection.
  3. RemoveAt(int index): This method takes the index position of the elements and remove that element from the collection.
  4. Insert(int index, Object value): This method is used to inserts an element into the collection at the specified index.
  5. 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:

ArrayList in C#

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:

  1. Fixed Length
  2. Cannot insert into the middle
  3. Cannot delete from middle

ArrayList:

  1. Variable Length
  2. Can insert into the middle of the collection
  3. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *