C# Arraylist

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.

C# ArrayList Declaration

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.

 

ArrayList arrList = new ArrayList();

If you observe the above arraylist declaration, we created a new arraylist (arrList) with an instance of arraylist class without specifying any size.

C# ArrayList Properties

The following are some of the commonly used properties of an arraylist in the c# programming language.

 

PropertyDescription
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.

C# ArrayList Methods

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.

 

MethodDescription
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.

C# ArrayList Example

Following is the example of using an arraylist in the c# programming language.

 

using System;
using System.Collections;

namespace Tutlane
{
    class Program
    {
       static void Main(string[] args)
       {
           ArrayList arrlist = new ArrayList();
           arrlist.Add("Welcome");
           arrlist.Add(100);
           arrlist.Add(20.5);
           arrlist.Add("Tutlane");
           Console.WriteLine("ArrayList Count: " + arrlist.Count);
           Console.WriteLine("ArrayList Capacity: " + arrlist.Capacity);
           Console.WriteLine("*********ArrayList Elements********");
           foreach (var item in arrlist)
           {
               Console.WriteLine(item);
           }
           Console.ReadLine();
       }
    }
}

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.

 

C# ArrayList Example Result

 

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.

C# Add Elements to ArrayList

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#.

 

using System;
using System.Collections;

namespace Tutlane
{
    class Program
    {
       static void Main(string[] args)
       {
          // creating arraylist
          ArrayList arrlist = new ArrayList();
          arrlist.Add("Welcome");
          arrlist.Add(100);
          arrlist.Add(20.5);
          arrlist.Add("Tutlane");
          //creating arraylist
          ArrayList arrlist2 = new ArrayList() { 10, "Hi" };
          // adding arrlist2 to arrlist
          arrlist.AddRange(arrlist2);
          Console.WriteLine("*********ArrayList Elements********");
          foreach (var item in arrlist)
          {
             Console.WriteLine(item);
          }
          Console.ReadLine();
       }
    }
}

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.

 

C# Add Elements to ArrayList Example Result

C# Access ArrayList Elements

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.

 

using System;
using System.Collections;

namespace Tutlane
{
    class Program
    {
       static void Main(string[] args)
       {
          // creating arraylist
          ArrayList arrlist = new ArrayList();
          arrlist.Add("Welcome");
          arrlist.Add(100);
          arrlist.Add(20.5f);
          arrlist.Add("Tutlane");
          // accessing the first element
          string msg = (string)arrlist[0];
          //Accessing the third element
          float num = (float)arrlist[2];
          Console.WriteLine("*********Access Elements with Index Position********");
          Console.WriteLine("Element at 0: " + msg);
          Console.WriteLine("Element at 2: " + num);
          Console.WriteLine("*********Access Elements with For Loop********");
          // for loop to access arraylist elements
          for (int i = 0; i < arrlist.Count; i++)
          {
              Console.WriteLine(arrlist[i]);
          }
          Console.WriteLine("*********Access Elements with Foreach Loop********");
          // foreach loop to access arraylist elements
          foreach (var item in arrlist)
          {
             Console.WriteLine(item);
          }
          Console.ReadLine();
       }
    }
}

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.

 

C# Access ArrayList Elements Example Result

C# Insert Elements into ArrayList

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#.

 

using System;
using System.Collections;

namespace Tutlane
{
    class Program
    {
       static void Main(string[] args)
       {
          // creating arraylist
          ArrayList arrlist = new ArrayList();
          arrlist.Add("Welcome");
          arrlist.Add(100);
          arrlist.Add(20.5f);
          arrlist.Add("Tutlane");
          // inserting elements into arraylist
          arrlist.Insert(0, "Hi");
          arrlist.Insert(1, 50);
          ArrayList arrlist2 = new ArrayList() { 200, 300 };
          // inserting arrlist2 into arrlist at position 2
          arrlist.InsertRange(2, arrlist2);
          Console.WriteLine("*********ArrayList Elements********");
          foreach (var item in arrlist)
          {
             Console.WriteLine(item);
          }
          Console.ReadLine();
       }
    }
}

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.

 

C# Insert Elements into ArrayList Example Result

C# Remove Elements from ArrayList

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.

 

using System;
using System.Collections;

namespace Tutlane
{
    class Program
    {
       static void Main(string[] args)
       {
          // creating arraylist
          ArrayList arrlist = new ArrayList();
          arrlist.Add("Welcome");
          arrlist.Add(100);
          arrlist.Add(20.5f);
          arrlist.Add("Tutlane");
          arrlist.Add(200);
          arrlist.Add(300);
          arrlist.Add(400);
          // Removing an element which is having a value 20.5f
          arrlist.Remove(20.5f);
          // Removing an element at index 0
          arrlist.RemoveAt(0);
          // Removing 2 elements starting from index 3
          arrlist.RemoveRange(3, 2);
          Console.WriteLine("*********ArrayList Elements********");
          foreach (var item in arrlist)
          {
             Console.WriteLine(item);
          }
          Console.ReadLine();
       }
    }
}

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.

 

C# Remove Elements from ArrayList Example Result

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.

C# ArrayList Check If Item Exists

 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#.

 

using System;
using System.Collections;

namespace Tutlane
{
    class Program
    {
       static void Main(string[] args)
       {
          // creating arraylist
          ArrayList arrlist = new ArrayList();
          arrlist.Add("Welcome");
          arrlist.Add(100);
          arrlist.Add(20.5f);
          arrlist.Add("Tutlane");
          // check for an item 100 exists in arraylist or not
          Console.WriteLine("Item Exists: " + arrlist.Contains(100));
          Console.ReadLine();
       }
    }
}

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.

 

Item Exists: True

C# ArrayList Overview

The following are the important points that need to remember about arraylist in c#.

 

  • ArrayList is used to store elements of different data types, and the size of the arraylist can grow or shrink dynamically by adding or removing elements.
  • ArrayList is a non-generic type of collection and available with a System.Collections namespace.
  • You can access ArrayList elements either using loops (for and foreach) or with a particular index position.
  • You need to cast ArrayList items to appropriate data types before using them in your application.
  • ArrayList provided different methods to perform multiple operations like add, insert, delete, sort, reverse, etc., on elements of ArrayList.

C# Difference between Array and ArrayList

The following are the main difference between array and arraylist in the c# programming language.

 

  • The array is strongly typed, which means it can store only the same data type elements, but you can store elements of different data types in ArrayList.
  • The array size is fixed, whereas the size of ArrayList will vary dynamically based on requirements.
  • ArrayList provided different methods to perform operations like add, insert, remove, etc., on multiple elements, but in the array, you can get or set the value of only one element at a time.
  • In c#, an array can have multiple dimensions, but an ArrayList will have only one dimension.
  • To use arraylist items, you need to cast them to the appropriate data type, but in the array, you don’t need to cast elements because it’s strongly typed.