top

Search

C# Tutorial

The ArrayList Collection implements the IList Interface and uses an array that is dynamic. The array resized automatically as required and dynamic memory allocation is allowed. The ArrayList Collection is an alternative of arrays.Constructors in ArrayList CollectionThe different constructors and their description is given as follows:Table: Constructors in ArrayList in C#Source: MSDNConstructorsDescriptionArrayList()This constructor initializes a new instance of the ArrayList class that is empty and has the default initial capacity.ArrayList(ICollection)This constructor initializes a new instance of the ArrayList class that contains elements copied from the specified collection and that has the same initial capacity as the number of elements copiedArrayList(Int32)This constructor initializes a new instance of the ArrayList class that is empty and has the specified initial capacity.Properties in ArrayList CollectionThe different properties and their description is given as follows:Table: Properties in ArrayList in C#Source: MSDNPropertiesDescriptionCapacityThis property gets or sets the number of elements that the ArrayList can contain.CountThis property gets  the number of elements actually contained in the ArrayList.IsFixedSizeThis property gets  a value indicating whether the ArrayList has a fixed size.IsReadOnlyThis property gets a value indicating whether the ArrayList is read-only.IsSynchronizedThis property gets a value indicating whether access to the ArrayList is synchronized (thread safe).Item[Int32]This property gets or sets the element at the specified index.SyncRootThis property gets an object that can be used to synchronize access to the ArrayList.Methods in ArrayList CollectionThe different methods and their description is given as follows:Table: Methods in ArrayList in C#Source: MSDNMethodsDescriptionAdapter(IList)This method creates an ArrayList wrapper for a specific IList.Add(Object)This method adds an object to the end of the ArrayList.BinarySearch(Object)This method searches the entire sorted ArrayList for an element using the default comparer and returns the zero-based index of the element.BinarySearch(Object, IComparer)This method searches the entire sorted ArrayList for an element using the specified comparer and returns the zero-based index of the element.Clear()This method removes all elements from the ArrayList.Clone()This method creates a shallow copy of the ArrayList.Contains(Object)This method determines whether an element is in the ArrayList.CopyTo(Array)This method copies the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array.CopyTo(Array, Int32)This method copies the entire ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array.Equals(Object)This method determines whether the specified object is equal to the current object.(Inherited from Object)FixedSize(ArrayList)This method returns an ArrayList wrapper with a fixed size.FixedSize(IList)This method returns an IList wrapper with a fixed size.GetEnumerator()This method returns an enumerator for the entire ArrayList.GetEnumerator(Int32, Int32)This method returns an enumerator for a range of elements in the ArrayList.GetHashCode()This method serves as the default hash function.GetRange(Int32, Int32)This method returns an ArrayList which represents a subset of the elements in the source ArrayList.GetType()This method gets the Type of the current instance.IndexOf(Object)This method searches for the specified Object and returns the zero-based index of the first occurrence within the entire ArrayList.IndexOf(Object, Int32)This method searches for the specified Object and returns the zero-based index of the first occurrence within the range of elements in the ArrayList that extends from the specified index to the last element.IndexOf(Object, Int32, Int32)This method searches for the specified Object and returns the zero-based index of the first occurrence within the range of elements in the ArrayList that starts at the specified index and contains the specified number of elements.Insert(Int32, Object)This method inserts an element into the ArrayList at the specified index.ReadOnly(ArrayList)This method returns a read-only ArrayList wrapper.ReadOnly(IList)This method returns a read-only IList wrapper.Remove(Object)This method removes the first occurrence of a specific object from the ArrayList.Repeat(Object, Int32)This method returns an ArrayList whose elements are copies of the specified value.Reverse()This method reverses the order of the elements in the entire ArrayList.Sort()This method sorts the elements in the entire ArrayList.Sort(IComparer)This method sorts the elements in the entire ArrayList using the specified comparer.Sort(Int32, Int32, IComparer)This method sorts the elements in a range of elements in ArrayList using the specified comparer.Synchronized(ArrayList)This method returns an ArrayList wrapper that is synchronized (thread safe).ToArray()This method copies the elements of the ArrayList to a new Object array.ToArray(Type)This method copies the elements of the ArrayList to a new array of the specified element type.ToString()This method returns a string that represents the current object.TrimToSize()This method sets the capacity to the actual number of elements in the ArrayList.ArrayList OperationsSome of the SortedList operations are given as follows:Adding elements in ArrayListElements can be added to the ArrayList using the Add() method. It adds the specified values to the ArrayList. The program that demonstrates this is given as follows:Source Code: Program to add elements in ArrayList in C#using System; using System.Collections; namespace ArrayListDemo {   class Example   {      static void Main(string[] args)      {         ArrayList arr = new ArrayList();         arr.Add(5);         arr.Add(1);         arr.Add(9);         arr.Add(6);         arr.Add(2);         arr.Add(8);         arr.Add(3);         arr.Add(7);         Console.Write("ArrayList elements are: ");         foreach (int i in arr)         {            Console.Write(i + " ");         }      }   } }The output of the above program is given below:ArrayList elements are: 5 1 9 6 2 8 3 7Deleting elements from ArrayListAn element can be deleted from the ArrayList using the Remove() method. This method deletes the first occurance of the value that is provided. The program that demonstrates this is given as follows:Source Code: Program to delete elements from ArrayList in C#using System; using System.Collections; namespace ArrayListDemo {   class Example   {      static void Main(string[] args)      {         ArrayList arr = new ArrayList();         arr.Add(5);         arr.Add(1);         arr.Add(9);         arr.Add(6);         arr.Add(2);         arr.Add(8);         arr.Add(3);         arr.Add(7);         Console.Write("Original ArrayList elements: ");         foreach (int i in arr)         {            Console.Write(i + " ");         }         arr.Remove(1);         arr.Remove(8);         arr.Remove(5);         Console.WriteLine();         Console.Write("ArrayList elements after deletion: ");         foreach (int i in arr)         {            Console.Write(i + " ");         }      }   } }The output of the above program is given below:Original ArrayList elements: 5 1 9 6 2 8 3 7 ArrayList elements after deletion: 9 6 2 3 7Reversing elements in ArrayListThe Reverse() method is used to reverse the order of the ArrayList elements. The program that demonstrates this is given as follows:Source Code: Program to reverse elements in ArrayList in C#using System; using System.Collections; namespace ArrayListDemo {   class Example   {      static void Main(string[] args)      {         ArrayList arr = new ArrayList();         arr.Add(5);         arr.Add(1);         arr.Add(9);         arr.Add(6);         arr.Add(2);         arr.Add(8);         arr.Add(3);         arr.Add(7);         Console.Write("ArrayList elements: ");         foreach (int i in arr)         {            Console.Write(i + " ");         }        // reversing array         arr.Reverse();         Console.WriteLine();         Console.Write("Reversed ArrayList elements: ");         foreach (int i in arr)         {            Console.Write(i + " ");         }      }   } }The output of the above program is given below:ArrayList elements: 5 1 9 6 2 8 3 7 Reversed ArrayList elements: 7 3 8 2 6 9 1 5Sorting elements in ArrayListThe Sort() method is used to sort the ArrayList elements. The program that demonstrates this is given as follows:Source Code: Program to sort elements in ArrayList in C#using System; using System.Collections; namespace ArrayListDemo {   class Example   {      static void Main(string[] args)      {         ArrayList arr = new ArrayList();         arr.Add(5);         arr.Add(1);         arr.Add(9);         arr.Add(6);         arr.Add(2);         arr.Add(8);         arr.Add(3);         arr.Add(7);         Console.Write("ArrayList elements: ");         foreach (int i in arr)         {            Console.Write(i + " ");         }         arr.Sort();         Console.WriteLine();         Console.Write("Sorted ArrayList elements: ");         foreach (int i in arr)         {            Console.Write(i + " ");         }      }   } }The output of the above program is given below:ArrayList elements: 5 1 9 6 2 8 3 7 Sorted ArrayList elements: 1 2 3 5 6 7 8 9Conversion of ArrayList to ArrayTo convert an ArrayList to Array, the following is the code:Source Code: Program to convert an ArrayList to Array in C#using System; using System.Collections; public class Example {    public static void Main()    {        ArrayList arr = new ArrayList();        arr.Add("AB");        arr.Add("BC");        arr.Add("DE");        arr.Add("FG");        arr.Add("HI");        arr.Add("JK");        string[] myArr = arr.ToArray(typeof(string)) as string[];        foreach (string a in myArr)        {            Console.WriteLine(a);        }    } }The above program gives the following output:AB BC DE FG HI JKRemove an element from an ArrayList at a position specified by userAt times, you may want to delete an element at a particular position in an ArrayList. For that, use the RemoveAt() method:Source Code: Program to delete an element at a particular position in an ArrayList in C#using System;   using System.Collections.Generic;   public class Example   {      public static void Main(string[] args)      {        var l = new List<string>();          l.Add("One");          l.Add("Two");          l.Add("Three");          foreach (var arr in l)          {           Console.WriteLine(arr);          }        l.RemoveAt(1);        Console.WriteLine("New list after deleting a single element:");        foreach (var arr in l)          {           Console.WriteLine(arr);          }    } }The above program gives the following output:One Two Three New list after deleting a single element: One ThreeCheck whether the ArrayList has a fixed size or notUse the IsFixedSize() property in C# to get a value stating whether the ArrayList has a fixed size or notLet us see an example:Source Code: Program to check whether the ArrayList has a fixed size or not in C#using System; using System.Collections; namespace ArrayListDemo {   class Example   {      static void Main(string[] args)      {         ArrayList arr = new ArrayList();         arr.Add(100);         arr.Add(120);         arr.Add(90);         arr.Add(60);         arr.Add(200);         arr.Add(110);         arr.Add(130);         arr.Add(180);         arr.Add(195);         arr.Add(165);         arr.Add(80);         Console.Write("ArrayList elements = ");         foreach (int i in arr)         {            Console.Write(i + " ");         }         Console.WriteLine("\nIsFixedSize? " + arr.IsFixedSize);      }   } }The above program gives the following output:ArrayList elements = 100 120 90 60 200 110 130 180 195 165 80 IsFixedSize? FalseCheck whether the ArrayList is read only or notUse the IsFixedSize() property in C# to get a value stating whether the ArrayList is read only or notLet us see an example:Source Code: Program to check whether the ArrayList is read only or not in C#using System; using System.Collections; namespace ArrayListDemo {   class Example   {      static void Main(string[] args)      {         ArrayList arr = new ArrayList();         arr.Add(5);         arr.Add(1);         arr.Add(9);         arr.Add(6);         arr.Add(2);         arr.Add(8);         arr.Add(3);         arr.Add(7);         Console.Write("ArrayList elements = ");         foreach (int i in arr)         {            Console.Write(i + " ");         }         Console.WriteLine("\nIsReadOnly? " + arr.IsReadOnly);      }   } }The above program gives the following output:ArrayList elements = 5 1 9 6 2 8 3 7 IsReadOnly? FalseGet the maximum size of the ArrayListUse the Capacity property to get the maximum size of the ArrayList. Let us now see an example:Source Code: Program to get the maximum size of the ArrayList in C#using System; using System.Collections; namespace ArrayListDemo {   class Example   {      static void Main(string[] args)      {         ArrayList arr = new ArrayList();         arr.Add(100);         arr.Add(120);         arr.Add(90);         arr.Add(60);         arr.Add(200);         arr.Add(110);         arr.Add(130);         arr.Add(180);         arr.Add(195);         arr.Add(165);         arr.Add(80);         Console.Write("ArrayList elements = ");         foreach (int i in arr)         {            Console.Write(i + " ");         }         Console.WriteLine("\nCapacity = " + arr.Capacity);      }   } }The above program gives the following output:ArrayList elements = 100 120 90 60 200 110 130 180 195 165 80 Capacity = 16Count the number of elements in a ArrayListUse the Count property to count the number of elements in a ArrayList . Let us see an example:Source Code: Program to count the number of elements in the ArrayList in C#using System; using System.Collections; namespace ArrayListDemo {   class Example   {      static void Main(string[] args)      {         ArrayList arr = new ArrayList();         arr.Add(100);         arr.Add(120);         arr.Add(90);         arr.Add(60);         arr.Add(200);         arr.Add(110);         arr.Add(130);         arr.Add(180);         arr.Add(195);         arr.Add(165);         arr.Add(80);         arr.Add(99);         arr.Add(145);         arr.Add(198);         arr.Add(210);         Console.Write("ArrayList elements = ");         foreach (int i in arr)         {            Console.Write(i + " ");         }         Console.WriteLine("\n\nCount = " + arr.Count);         Console.WriteLine("Capacity = " + arr.Capacity);      }   } }The above program gives the following output:ArrayList elements = 100 120 90 60 200 110 130 180 195 165 80 99 145 198 210 Count = 15 Capacity = 16Fetch an element at a specified position in ArrayListAt times, you may need to fetch an element at a specified position in ArrayList. For that, the following is the code:Source Code: Program to fetch an element at a specified position in ArrayList in C#using System; using System.Collections; namespace ArrayListDemo {   class Example   {      static void Main(string[] args)      {         ArrayList arr = new ArrayList();         arr.Add(100);         arr.Add(120);         arr.Add(90);         arr.Add(60);         arr.Add(200);         arr.Add(110);         arr.Add(130);         arr.Add(180);         arr.Add(195);         arr.Add(165);         arr.Add(80);         arr.Add(99);         arr.Add(145);         arr.Add(198);         arr.Add(210);         Console.Write("ArrayList elements = ");         foreach (int i in arr)         {            Console.Write(i + " ");         }         Console.WriteLine("\n\nCount = " + arr.Count);         Console.WriteLine("Capacity = " + arr.Capacity);         Console.WriteLine("Element {0} is \"{1}\"", 2+"th", arr[1]);         Console.WriteLine("Element {0} is \"{1}\"", 4+"th", arr[3]);         Console.WriteLine("Element {0} is \"{1}\"", 6+"th", arr[5]);         Console.WriteLine("Element {0} is \"{1}\"", 8+"th", arr[7]);      }   } }The above program generates the following output:ArrayList elements = 100 120 90 60 200 110 130 180 195 165 80 99 145 198 210 Count = 15 Capacity = 16 Element 2th is "120" Element 4th is "60" Element 6th is "110" Element 8th is "180"
logo

C# Tutorial

ArrayList Collection on C#

The ArrayList Collection implements the IList Interface and uses an array that is dynamic. The array resized automatically as required and dynamic memory allocation is allowed. The ArrayList Collection is an alternative of arrays.

Constructors in ArrayList Collection

The different constructors and their description is given as follows:

Table: Constructors in ArrayList in C#

Source: MSDN

ConstructorsDescription
ArrayList()This constructor initializes a new instance of the ArrayList class that is empty and has the default initial capacity.
ArrayList(ICollection)

This constructor initializes a new instance of the ArrayList class that contains elements copied from the specified collection and that has the same initial capacity as the number of elements copied
ArrayList(Int32)This constructor initializes a new instance of the ArrayList class that is empty and has the specified initial capacity.

Properties in ArrayList Collection

The different properties and their description is given as follows:

Table: Properties in ArrayList in C#

Source: MSDN

PropertiesDescription
CapacityThis property gets or sets the number of elements that the ArrayList can contain.
CountThis property gets  the number of elements actually contained in the ArrayList.
IsFixedSizeThis property gets  a value indicating whether the ArrayList has a fixed size.
IsReadOnlyThis property gets a value indicating whether the ArrayList is read-only.
IsSynchronizedThis property gets a value indicating whether access to the ArrayList is synchronized (thread safe).
Item[Int32]This property gets or sets the element at the specified index.
SyncRootThis property gets an object that can be used to synchronize access to the ArrayList.

Methods in ArrayList Collection

The different methods and their description is given as follows:

Table: Methods in ArrayList in C#

Source: MSDN

MethodsDescription
Adapter(IList)This method creates an ArrayList wrapper for a specific IList.
Add(Object)This method adds an object to the end of the ArrayList.
BinarySearch(Object)This method searches the entire sorted ArrayList for an element using the default comparer and returns the zero-based index of the element.
BinarySearch(Object, IComparer)This method searches the entire sorted ArrayList for an element using the specified comparer and returns the zero-based index of the element.
Clear()This method removes all elements from the ArrayList.
Clone()This method creates a shallow copy of the ArrayList.
Contains(Object)This method determines whether an element is in the ArrayList.
CopyTo(Array)This method copies the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array.
CopyTo(Array, Int32)This method copies the entire ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array.
Equals(Object)This method determines whether the specified object is equal to the current object.
(Inherited from Object)
FixedSize(ArrayList)This method returns an ArrayList wrapper with a fixed size.
FixedSize(IList)This method returns an IList wrapper with a fixed size.
GetEnumerator()This method returns an enumerator for the entire ArrayList.
GetEnumerator(Int32, Int32)This method returns an enumerator for a range of elements in the ArrayList.
GetHashCode()This method serves as the default hash function.
GetRange(Int32, Int32)This method returns an ArrayList which represents a subset of the elements in the source ArrayList.
GetType()This method gets the Type of the current instance.
IndexOf(Object)This method searches for the specified Object and returns the zero-based index of the first occurrence within the entire ArrayList.
IndexOf(Object, Int32)This method searches for the specified Object and returns the zero-based index of the first occurrence within the range of elements in the ArrayList that extends from the specified index to the last element.
IndexOf(Object, Int32, Int32)This method searches for the specified Object and returns the zero-based index of the first occurrence within the range of elements in the ArrayList that starts at the specified index and contains the specified number of elements.
Insert(Int32, Object)This method inserts an element into the ArrayList at the specified index.
ReadOnly(ArrayList)This method returns a read-only ArrayList wrapper.
ReadOnly(IList)This method returns a read-only IList wrapper.
Remove(Object)This method removes the first occurrence of a specific object from the ArrayList.
Repeat(Object, Int32)This method returns an ArrayList whose elements are copies of the specified value.
Reverse()This method reverses the order of the elements in the entire ArrayList.
Sort()This method sorts the elements in the entire ArrayList.
Sort(IComparer)This method sorts the elements in the entire ArrayList using the specified comparer.
Sort(Int32, Int32, IComparer)This method sorts the elements in a range of elements in ArrayList using the specified comparer.
Synchronized(ArrayList)This method returns an ArrayList wrapper that is synchronized (thread safe).
ToArray()This method copies the elements of the ArrayList to a new Object array.
ToArray(Type)This method copies the elements of the ArrayList to a new array of the specified element type.
ToString()This method returns a string that represents the current object.
TrimToSize()This method sets the capacity to the actual number of elements in the ArrayList.

ArrayList Operations

Some of the SortedList operations are given as follows:

Adding elements in ArrayList

Elements can be added to the ArrayList using the Add() method. It adds the specified values to the ArrayList. The program that demonstrates this is given as follows:

Source Code: Program to add elements in ArrayList in C#

using System;
using System.Collections;
namespace ArrayListDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        ArrayList arr = new ArrayList();
        arr.Add(5);
        arr.Add(1);
        arr.Add(9);
        arr.Add(6);
        arr.Add(2);
        arr.Add(8);
        arr.Add(3);
        arr.Add(7);
        Console.Write("ArrayList elements are: ");
        foreach (int i in arr)
        {
           Console.Write(i + " ");
        }
     }
  }
}

The output of the above program is given below:

ArrayList elements are: 5 1 9 6 2 8 3 7

Deleting elements from ArrayList

An element can be deleted from the ArrayList using the Remove() method. This method deletes the first occurance of the value that is provided. The program that demonstrates this is given as follows:

Source Code: Program to delete elements from ArrayList in C#

using System;
using System.Collections;
namespace ArrayListDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        ArrayList arr = new ArrayList();
        arr.Add(5);
        arr.Add(1);
        arr.Add(9);
        arr.Add(6);
        arr.Add(2);
        arr.Add(8);
        arr.Add(3);
        arr.Add(7);
        Console.Write("Original ArrayList elements: ");
        foreach (int i in arr)
        {
           Console.Write(i + " ");
        }
        arr.Remove(1);
        arr.Remove(8);
        arr.Remove(5);
        Console.WriteLine();
        Console.Write("ArrayList elements after deletion: ");
        foreach (int i in arr)
        {
           Console.Write(i + " ");
        }
     }
  }
}

The output of the above program is given below:

Original ArrayList elements: 5 1 9 6 2 8 3 7
ArrayList elements after deletion: 9 6 2 3 7

Reversing elements in ArrayList

The Reverse() method is used to reverse the order of the ArrayList elements. The program that demonstrates this is given as follows:

Source Code: Program to reverse elements in ArrayList in C#

using System;
using System.Collections;
namespace ArrayListDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        ArrayList arr = new ArrayList();
        arr.Add(5);
        arr.Add(1);
        arr.Add(9);
        arr.Add(6);
        arr.Add(2);
        arr.Add(8);
        arr.Add(3);
        arr.Add(7);
        Console.Write("ArrayList elements: ");
        foreach (int i in arr)
        {
           Console.Write(i + " ");
        }
       // reversing array
        arr.Reverse();
        Console.WriteLine();
        Console.Write("Reversed ArrayList elements: ");
        foreach (int i in arr)
        {
           Console.Write(i + " ");
        }
     }
  }
}

The output of the above program is given below:

ArrayList elements: 5 1 9 6 2 8 3 7
Reversed ArrayList elements: 7 3 8 2 6 9 1 5

Sorting elements in ArrayList

The Sort() method is used to sort the ArrayList elements. The program that demonstrates this is given as follows:

Source Code: Program to sort elements in ArrayList in C#

using System;
using System.Collections;
namespace ArrayListDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        ArrayList arr = new ArrayList();
        arr.Add(5);
        arr.Add(1);
        arr.Add(9);
        arr.Add(6);
        arr.Add(2);
        arr.Add(8);
        arr.Add(3);
        arr.Add(7);
        Console.Write("ArrayList elements: ");
        foreach (int i in arr)
        {
           Console.Write(i + " ");
        }
        arr.Sort();
        Console.WriteLine();
        Console.Write("Sorted ArrayList elements: ");
        foreach (int i in arr)
        {
           Console.Write(i + " ");
        }
     }
  }
}

The output of the above program is given below:

ArrayList elements: 5 1 9 6 2 8 3 7
Sorted ArrayList elements: 1 2 3 5 6 7 8 9

Conversion of ArrayList to Array

To convert an ArrayList to Array, the following is the code:

Source Code: Program to convert an ArrayList to Array in C#

using System;
using System.Collections;
public class Example
{
   public static void Main()
   {
       ArrayList arr = new ArrayList();
       arr.Add("AB");
       arr.Add("BC");
       arr.Add("DE");
       arr.Add("FG");
       arr.Add("HI");
       arr.Add("JK");
       string[] myArr = arr.ToArray(typeof(string)) as string[];
       foreach (string a in myArr)
       {
           Console.WriteLine(a);
       }
   }
}

The above program gives the following output:

AB
BC
DE
FG
HI
JK

Remove an element from an ArrayList at a position specified by user

At times, you may want to delete an element at a particular position in an ArrayList. For that, use the RemoveAt() method:

Source Code: Program to delete an element at a particular position in an ArrayList in C#

using System;  
using System.Collections.Generic;  
public class Example  
{  
   public static void Main(string[] args)  
   {
       var l = new List<string>();  
       l.Add("One");  
       l.Add("Two");  
       l.Add("Three");  
       foreach (var arr in l)  
       {
          Console.WriteLine(arr);  
       }
       l.RemoveAt(1);
       Console.WriteLine("New list after deleting a single element:");
       foreach (var arr in l)  
       {
          Console.WriteLine(arr);  
       }
   }
}

The above program gives the following output:

One
Two
Three
New list after deleting a single element:
One
Three

Check whether the ArrayList has a fixed size or not

Use the IsFixedSize() property in C# to get a value stating whether the ArrayList has a fixed size or not

Let us see an example:

Source Code: Program to check whether the ArrayList has a fixed size or not in C#

using System;
using System.Collections;
namespace ArrayListDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        ArrayList arr = new ArrayList();
        arr.Add(100);
        arr.Add(120);
        arr.Add(90);
        arr.Add(60);
        arr.Add(200);
        arr.Add(110);
        arr.Add(130);
        arr.Add(180);
        arr.Add(195);
        arr.Add(165);
        arr.Add(80);
        Console.Write("ArrayList elements = ");
        foreach (int i in arr)
        {
           Console.Write(i + " ");
        }
        Console.WriteLine("\nIsFixedSize? " + arr.IsFixedSize);
     }
  }
}

The above program gives the following output:

ArrayList elements = 100 120 90 60 200 110 130 180 195 165 80
IsFixedSize? False

Check whether the ArrayList is read only or not

Use the IsFixedSize() property in C# to get a value stating whether the ArrayList is read only or not

Let us see an example:

Source Code: Program to check whether the ArrayList is read only or not in C#

using System;
using System.Collections;
namespace ArrayListDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        ArrayList arr = new ArrayList();
        arr.Add(5);
        arr.Add(1);
        arr.Add(9);
        arr.Add(6);
        arr.Add(2);
        arr.Add(8);
        arr.Add(3);
        arr.Add(7);
        Console.Write("ArrayList elements = ");
        foreach (int i in arr)
        {
           Console.Write(i + " ");
        }
        Console.WriteLine("\nIsReadOnly? " + arr.IsReadOnly);
     }
  }
}

The above program gives the following output:

ArrayList elements = 5 1 9 6 2 8 3 7
IsReadOnly? False

Get the maximum size of the ArrayList

Use the Capacity property to get the maximum size of the ArrayList. Let us now see an example:

Source Code: Program to get the maximum size of the ArrayList in C#

using System;
using System.Collections;
namespace ArrayListDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        ArrayList arr = new ArrayList();
        arr.Add(100);
        arr.Add(120);
        arr.Add(90);
        arr.Add(60);
        arr.Add(200);
        arr.Add(110);
        arr.Add(130);
        arr.Add(180);
        arr.Add(195);
        arr.Add(165);
        arr.Add(80);
        Console.Write("ArrayList elements = ");
        foreach (int i in arr)
        {
           Console.Write(i + " ");
        }
        Console.WriteLine("\nCapacity = " + arr.Capacity);
     }
  }
}

The above program gives the following output:

ArrayList elements = 100 120 90 60 200 110 130 180 195 165 80
Capacity = 16

Count the number of elements in a ArrayList

Use the Count property to count the number of elements in a ArrayList . Let us see an example:

Source Code: Program to count the number of elements in the ArrayList in C#

using System;
using System.Collections;
namespace ArrayListDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        ArrayList arr = new ArrayList();
        arr.Add(100);
        arr.Add(120);
        arr.Add(90);
        arr.Add(60);
        arr.Add(200);
        arr.Add(110);
        arr.Add(130);
        arr.Add(180);
        arr.Add(195);
        arr.Add(165);
        arr.Add(80);
        arr.Add(99);
        arr.Add(145);
        arr.Add(198);
        arr.Add(210);
        Console.Write("ArrayList elements = ");
        foreach (int i in arr)
        {
           Console.Write(i + " ");
        }
        Console.WriteLine("\n\nCount = " + arr.Count);
        Console.WriteLine("Capacity = " + arr.Capacity);
     }
  }
}

The above program gives the following output:

ArrayList elements = 100 120 90 60 200 110 130 180 195 165 80 99 145 198 210
Count = 15
Capacity = 16

Fetch an element at a specified position in ArrayList

At times, you may need to fetch an element at a specified position in ArrayList. For that, the following is the code:

Source Code: Program to fetch an element at a specified position in ArrayList in C#

using System;
using System.Collections;
namespace ArrayListDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        ArrayList arr = new ArrayList();
        arr.Add(100);
        arr.Add(120);
        arr.Add(90);
        arr.Add(60);
        arr.Add(200);
        arr.Add(110);
        arr.Add(130);
        arr.Add(180);
        arr.Add(195);
        arr.Add(165);
        arr.Add(80);
        arr.Add(99);
        arr.Add(145);
        arr.Add(198);
        arr.Add(210);
        Console.Write("ArrayList elements = ");
        foreach (int i in arr)
        {
           Console.Write(i + " ");
        }
        Console.WriteLine("\n\nCount = " + arr.Count);
        Console.WriteLine("Capacity = " + arr.Capacity);
        Console.WriteLine("Element {0} is \"{1}\"", 2+"th", arr[1]);
        Console.WriteLine("Element {0} is \"{1}\"", 4+"th", arr[3]);
        Console.WriteLine("Element {0} is \"{1}\"", 6+"th", arr[5]);
        Console.WriteLine("Element {0} is \"{1}\"", 8+"th", arr[7]);
     }
  }
}

The above program generates the following output:

ArrayList elements = 100 120 90 60 200 110 130 180 195 165 80 99 145 198 210
Count = 15
Capacity = 16
Element 2th is "120"
Element 4th is "60"
Element 6th is "110"
Element 8th is "180"

Leave a Reply

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

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial