Getsuyodev, Programming is mainly used when constructing an application. Programming requires knowledge of application domains, algorithms, and programming language expertise. Programming can be developed in different logic based on developer knowledge.

How to use C# ArrayList Class



ArrayList is one of the most flexible data structure from CSharp Collections. ArrayList contains a simple list of values. ArrayList implements the IList interface using an array, and very quickly we can add, insert, delete, view etc. It is very flexible because we can add without any size information, that is it will grow dynamically and also shrink.

C# ArrayList examples

  • Add an Item in an ArrayList
  • Insert an Item in a specified position in an ArrayList
  • Remove an Item from ArrayList
  • Remove an item from a specified position
  • Remove a range of elements from the ArrayList
  • Sort Items in an ArrayList
  • Count and Capacity Properties
  • Convert an ArrayList to Array
  • When to use array[] over ArrayList

How to add an Item in an ArrayList?

Syntax

ArrayList.add(object)
  • object: The Item to add the ArrayList

The Add() method of ArrayList is used to add new items in ArrayList.

example

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      ArrayList arrList = new ArrayList();
      arrList.Add("North");
      arrList.Add("East");
      arrList.Add("West");
      arrList.Add("South");
      Console.WriteLine("ArrayList Elements");
      foreach (object item in arrList)
      {
          Console.WriteLine(item);
      }
      Console.ReadKey();
    }
  }
}

How to Insert an Item in an ArrayList?

Elements can be added or removed from the C# ArrayList collection at any point in time. ArrayList.Insert(Int32, Object) method inserts an element into the ArrayList at the specified index.

Syntax

ArrayList.insert(index,object)
  • index: The position of the item in an ArrayList
  • object: The Item to add the ArrayList

example

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      ArrayList arrList = new ArrayList();
      arrList.Add("North");
      arrList.Add("West");
      arrList.Add("South");
      Console.WriteLine("ArrayList Elements...");
      foreach (object item in arrList)
      {
          Console.WriteLine(item);
      }
      //insert an item at the top of arraylist
      arrList.Insert(0, "Directions");
      //insert an item at the 3rd position  of arraylist
      arrList.Insert(2, "East");
      Console.WriteLine("ArrayList Elements after insert...");
      foreach (object item in arrList)
      {
          Console.WriteLine(item);
      }
      Console.ReadKey();
    }
  }
}

How to remove an item from ArrayList?

ArrayList remove() method removes the first occurrence of a specific object from the ArrayList.

Syntax

 
ArrayList.Remove(object)
  • object: The Item to add the ArrayList

example

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      ArrayList arrList = new ArrayList();
      arrList.Add("North");
      arrList.Add("East");
      arrList.Add("West");
      arrList.Add("South");
      Console.WriteLine("ArrayList Elements...");
      foreach (object item in arrList)
      {
          Console.WriteLine(item);
      }
      //remove() method removes the first occurrence
      arrList.Remove("West");
      Console.WriteLine("ArrayList Elements after remove()...");
      foreach (object item in arrList)
      {
          Console.WriteLine(item);
      }
      Console.ReadKey();
    }
  }
}

Remove an item in a specified position from an ArrayList?

Arraylist ArrayList.RemoveAt(index) remove the specified index element from arraylist.

Syntax

ArrayList.RemoveAt(index)
  • index: the position of an item to remove from an ArrayList

example

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      ArrayList arrList = new ArrayList();
      arrList.Add("North");
      arrList.Add("East");
      arrList.Add("West");
      arrList.Add("South");
      Console.WriteLine("ArrayList Elements...");
      foreach (object item in arrList)
      {
          Console.WriteLine(item);
      }
      //remove() method removes the first occurrence
      arrList.Remove("West");
      Console.WriteLine("ArrayList Elements after remove()...");
      foreach (object item in arrList)
      {
          Console.WriteLine(item);
      }
      Console.ReadKey();
    }
  }
}

Arraylist RemoveRange

ArrayList RemoveRange(Int32, Int32) method is used to remove a range of elements from the ArrayList. The following program removes the range 1 to 2 elements from Arraylist

example

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      ArrayList arrList = new ArrayList();
      arrList.Add("North");
      arrList.Add("East");
      arrList.Add("West");
      arrList.Add("South");
      Console.WriteLine("ArrayList Elements...");
      foreach (object item in arrList)
      {
          Console.WriteLine(item);
      }
      //remove range of elements from arraylist
      arrList.RemoveRange(1,2);
      Console.WriteLine("ArrayList Elements after RemoveRange()...");
      foreach (object item in arrList)
      {
          Console.WriteLine(item);
      }
      Console.ReadKey();
    }
  }
}

How to sort ArrayList?

ArrayList Sort() method sorts the elements in the entire ArrayList.

Syntax

ArrayList.Sort()

example

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      ArrayList arrList = new ArrayList();
      arrList.Add("North");
      arrList.Add("East");
      arrList.Add("West");
      arrList.Add("South");
      Console.WriteLine("ArrayList Elements...");
      foreach (object item in arrList)
      {
          Console.WriteLine(item);
      }
      //sort arraylist elements
      arrList.Sort();
      Console.WriteLine("ArrayList Elements after sort()...");
      foreach (object item in arrList)
      {
          Console.WriteLine(item);
      }
      Console.ReadKey();
    }
  }
}

Arraylist Count and Capacity

The Count property returns the number of elements in an ArrayList, which is a non-generic collection that stores its length in a property called Count.

The Capacity property returns the number of elements that the ArrayList can store. Capacity is always greater than or equal to Count. If Count exceeds Capacity while adding elements, the Capacity is automatically increased by reallocating the internal array before copying the old elements and adding the new elements.

example

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      ArrayList arrList = new ArrayList();
      arrList.Add("Directions...");
      arrList.Add("North");
      arrList.Add("East");
      arrList.Add("West");
      arrList.Add("South");
      Console.WriteLine("ArrayList Elements...");
      foreach (object item in arrList)
      {
          Console.WriteLine(item);
      }
      Console.WriteLine("Count of elements of ArrayList  " + arrList.Count);
      Console.WriteLine("Current capacity of ArrayList  " + arrList.Capacity);
      Console.ReadKey();
    }
  }
}

Convert an ArrayList to Array

ArrayList ToArray() method returns an Object array containing copies of the elements of the ArrayList. The ToArray() method copy the elements of the ArrayList to a new Object array.

Syntax

public virtual object[] ToArray ();

example

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      ArrayList arrList = new ArrayList();
      arrList.Add("Directions...");
      arrList.Add("North");
      arrList.Add("East");
      arrList.Add("West");
      arrList.Add("South");
      Console.WriteLine("ArrayList Elements...");
      foreach (object item in arrList)
      {
          Console.WriteLine(item);
      }
      //converting arraylits to array
      object[] arrObj = arrList.ToArray();
      Console.WriteLine("Array Elements...");
      foreach (string str in arrObj)
      {
          Console.WriteLine(str);
      }
      Console.ReadKey();
    }
  }
}

When to use array[] over ArrayList

C# Arrays are strongly typed, and work well as parameters. If you know the length of your collection and it is fixed, it is better to use an array.


Comments are closed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More