Arraylist in C#

In c#, Arraylist is used to store an elements of different data types and the size of arraylist can grow or shrink dynamically based on the need of our application like adding or removing an elements from arraylist.

 

In c#, arraylists are same as an arrays but only difference is arrays are used to store a fixed number of 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, collection is a class so to define an arraylist, we must need to declare an instance of the arraylist class before we perform any operations like add, delete, etc. like as shown below.

 

ArrayList arrList = new ArrayList();

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

C# Arraylist Properties

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

 

PropertyDescription
Capacity It is used to get or set the number of elements an arraylist can contain.
Count It is used to get the number of elements in an arraylist.
IsFixedSize It is used to get a value to indicate that the arraylist has fixed size or not.
IsReadOnly It is used to get a value to indicate that the arraylist is readonly or not.
Item It is used get or set the element at the specified index.
IsSynchronized It is used to get a value to inidicate that an access to arraylist is synchronized (thread safe) or not.

C# Arraylist Methods

Following are the some of commonly used methods of an arraylist to add, search, insert, delete or sort an elements of arraylist in c# programming language.

 

MethodDescription
Add It is used to add an element at the end of arraylist.
AddRange It is used to add all the elements of specified collection at the end of arraylist.
Clear It will remove all the elements in arraylist.
Clone It will create a shallow copy of arraylist.
Contains It is used determine whether the specified element exists in arraylist or not.
CopyTo It is used to copy all the elements of an arraylist into another compatible array.
GetRange It is used to return a specified range of arraylist elements starting from the specified index.
IndexOf It is used to search for a specified element and return a zero-based index if found otherwise return -1 if no element found.
Insert It is used to insert an element in arraylist at the specified index.
InsertRange It is used to insert all the elements of specified collection into arraylist starting from the specified index.
Remove It is used to remove a first occurence of specified element from the arraylist.
RemoveAt It is used to remove an element from arraylist based on the specified index position.
RemoveRange It is used to remove a range of elements from arraylist.
Reverse It reverse the order of arraylist elements.
Sort It sorts the elements in an arraylist.
ToArray It copies all the elements of an arraylist to new array object.

C# Arraylist Example

Following is the example of using an arraylist in 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 above example, we are able to define a new arraylist (arrlist) collection by using System.Collections namespace. Here, we are binding a different data type values to newly created arraylist (arrlist) by using Add method and with Count & Capacity properties we are able to access number of elements in an arraylist.

 

When we execute above c# program, we will get the result like as shown below.

 

C# ArrayList Example Result

 

This is how we can use arraylist in c# to hold a group of different data type objects based on our requirements.

 

Following are the some of useful examples to work with arraylist in c# programming language.

C# Add Elements to ArrayList

In c#, by using Add or AddRange method, we can add a different data type elements to an arraylist. 

 

Following is the example of adding an 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 above example, we created a multiple arraylists (arrlist, arrlist2) and adding an elements to the arraylists in different ways.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Add Elements to ArrayList Example Result

C# Access ArrayList Elements

In c#, we have a different ways to access arraylist elements i.e. either by using index positions or by iterating through an arraylist using for / foreach loops.

 

Following is the example of accessing an 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 first element

            string msg = (string)arrlist[0];

            // accessing 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 above example, we are accessing arraylist elements in different ways by using index positions, for and foreach loops based on our requirements.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Access ArrayList Elements Example Result

C# Insert Elements into ArrayList

In c#, we can insert an elements into arraylist either by using Insert() or InsertRange() methods.

 

Following is the example of inserting an elements into 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 above example, we inserted an elements into arraylist at different index positions (0, 1) by using Insert() method. Same way, by using InsertRange() method we are inserting all the element of newly created arraylist (arrlist2) into arrlist at index position 2.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Insert Elements into ArrayList Example Result

C# Remove Elements from ArrayList

In c#, we have a different methods to remove an elements from arraylist i.e. either by using Remove() or RemoveAt() or RemoveRange() methods.

 

Following is the example of deleting an 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 above example, we used a Remove() method to delete particular value of element from arraylist and used a RemoveAt() method to delete an element at specified index position.

 

Same way, we used RemoveRange() method to delete specified number of elements from arraylist starting from the specified index position.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Remove Elements from ArrayList Example Result

In case, if you want to remove all the elements from arraylist, then use Clear() method.

 

Same way, we can use different methods like Sort() method to sort an elements of arraylist and Reverse() method to reverse the order of arraylist elements.

C# ArrayList Check If Item Exists

 By using Contain() method, we can check whether the specified element exists in arraylist or not. In case, if it exists it will return true otherwise false.

 

Following is the example of using Contain() method to check for an item 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 above example, we used a Contain() method to check for an item (100) exists in arraylist (arrlist) or not.

 

When we execute above c# program, we will get the result like as shown below.

 

Item Exists: True

C# ArrayList Overview

Following are important points which needs to remember about arraylist in c#.

 

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

C# Difference between Array and ArrayList

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

 

  • Array is a strongly typed which means it can store only the elements of same data type but in arraylist we can store an elements of different data types.
  • The size of array is fixed whereas the size of arraylist will vary dynamically based on requirements.
  • Arraylist provided a different methods to perform an operations like add, insert, remove, etc. on multiple elements but in array, we 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 an arraylist items, we need to cast it to appropriate data type but in array we don’t need to cast an elements because it’s a strongly type. 
 

PREVIOUS

C# Collections