ArrayList in C#

  • ArrayList is an index based ordered collection of objects.
  • It is like array, but like array its size is not fixed. It can dynamically grows or shrinks as per the requirement.
  • We can add as many Items as possible into arraylist till the memory is available.
  • As compared to Array, ArrayList is not type safe. Also it stores the objects into it, it is an overhead of boxing and unboxing of objects while adding,accessing items from ArrayList.

See below simple example of using ArrayList

+
Code
using System;
using System.Collections;
 
namespace Arraylist_Collection
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList arrlst = new ArrayList();
            
            arrlst.Add(1);
            arrlst.Add(2);
            arrlst.Add("one");
            arrlst.Add("two");
 
            foreach (object obj in arrlst)
            {
                Console.WriteLine(obj);
            }
            Console.ReadLine();
        }
    }
}

In above example, We have declared an ArrayList arrlst. By using Add method of ArrayList which requires object as a parameter, we added elements into it. We have added both integer elements and string elements into it. We accessed the elements of ArrayList using ForEach loop. We can use For loop as well for accessing the elements from ArrayList.
As we seen in above example, At a time we can store any object in ArrayList (in our case we stored Integer and String Item into ArrayList), It is not type safe.

See below example for now, where we have declared variable m of integer type in foreach loop to assign integer value from Arraylist to it and wrote this for loop in the try..catch block in order to capture error.
If you check the output below,  you will see the error Specified cast is not valid. after printing 'one'. There is a type cast error occurred while converting the string value 'one' into integer. This example indicates that ArrayList is not type safe as we store any object in it.

+
Code
using System;
using System.Collections;
 
namespace Arraylist_Collection
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList arrlst = new ArrayList();
            
            arrlst.Add(1);
            arrlst.Add(2);
            arrlst.Add("one");
            arrlst.Add("two");
 
            try
            {
                foreach (object obj in arrlst)
                {
                    Console.WriteLine(obj);
                    int m = (int)obj;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
}

ArrayList in C#

See below example with use of some basic and important functions of ArrayList.

+
Code
using System;
using System.Collections;
 
namespace Arraylist_Collection
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList arrUserList = new ArrayList();
 
            //Add function
            arrUserList.Add("Sameer");
            arrUserList.Add("Yuvraj");
            arrUserList.Add("Ramesh");
            arrUserList.Add("Amit");
            arrUserList.Add("Suresh");
 
            Console.WriteLine("Added users using Add\n");
            foreach (object obj in arrUserList)
            {
                Console.WriteLine(obj);
            }
 
            //AddRange Function
            string[] strUserList = { "Satish""Babita" };
 
            arrUserList.AddRange(strUserList);
 
            Console.WriteLine("\nAdded users using AddRange\n");
            foreach (object obj in arrUserList)
            {
                Console.WriteLine(obj);
            }
 
            //Count Function
            Console.WriteLine("\nCount of Elements of arraylist\n");
            Console.WriteLine("No of Elements : {0}", arrUserList.Count);
 
            //Remove function
            arrUserList.Remove("Babita");
 
            Console.WriteLine("\nRemoved perticular user using Remove function\n");
            foreach (object obj in arrUserList)
            {
                Console.WriteLine(obj);
            }
 
            //RemoveAt function
            arrUserList.RemoveAt(0);
 
            Console.WriteLine("\nRemoved user using RemoveAt function\n");
            foreach (object obj in arrUserList)
            {
                Console.WriteLine(obj);
            }
 
            //Contains Function
            Console.WriteLine("\nCheck if element exists or not using Contains function");
            if (arrUserList.Contains("Amitabh"))
                Console.WriteLine("Amitabh is in ArrayList");
            else
                Console.WriteLine("Amitabh is not in ArrayList");
 
            //Sort Function
            arrUserList.Sort();
 
            Console.WriteLine("\nArrayList after sorting\n");
            foreach (object obj in arrUserList)
            {
                Console.WriteLine(obj);
            }
 
            //Reverse Function
            arrUserList.Reverse();
 
            Console.WriteLine("\nArrayList after reversing\n");
            foreach (object obj in arrUserList)
            {
                Console.WriteLine(obj);
            }
 
            Console.ReadLine();
        }
    }
} 
 
 

Add : Adds the element at the last index of ArrayList
AddRange: This function Accepts any collection that implements ICollection Interface and adds the element at the end of Arraylist.
Count: Returns the number of Elements. 
Remove: Remove specified Item from ArrayList
RemoveAt: Remove Item located at specified Index.
Contains: Determine whether Specified Item present in ArrayList or not. Return true if present otherwise false.
Sort: Sort elements in the Ascending order.
Reverse: Reverse the order of Elements.


Below is the output of above program.

ArrayList in C# second example

No comments :