C# ArrayList - RemoveAt() Method



The C# ArrayList RemoveAt() method is used to remove or omit the element at the specified index position from the ArrayList.

Once the element is removed from the ArrayList, its size is adjusted, and the value of the count property is decreased by one.

Syntax

Following is the syntax of the C# ArrayList RemoveAt() method −

public virtual void RemoveAtAt (int index);

Parameters

This method accepts a single parameter −

  • index: It is a zero-based index of the element to remove.

Return value

This method does not return any value.

Example 1: Remove an Integer from ArrayList

Following is the basic example of the RemoveAt() method to remove an element at the specified index from ArrayList −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { 1, 2, 2, 3, 4 };
      
      Console.Write("Initial ArrayList: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      
      // remove an element at index 2
      arrayList.RemoveAt(2);
      
      Console.Write( "\nUpdated ArrayList: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Initial ArrayList: 1 2 2 3 4 
Updated ArrayList: 1 2 3 4

Example 2: Remove String from ArrayList

Let's see another example where we remove a string element at the specified index value from the ArrayList using the RemoveAt() method −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { "Hi", "tutorialspoint", "India", "tutorix" };
      
      Console.Write("Initial arrayList: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      
      // remove a string at 3
      arrayList.RemoveAt(3);
      
      Console.Write( "\nUpdated ArrayList: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Initial arrayList: Hi tutorialspoint India tutorix 
Updated ArrayList: Hi tutorialspoint India 

Example 3: Remove Both Integer and Character from ArrayList

The below example creates an ArrayList with some elements. We then use the RemoveAt() method to remove the specified element from ArrayList −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { "A", 1, 2, 4, "B", "C", "D" };
      
      Console.Write("Initial ArrayList: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      
      // remove an element at index 3
      arrayList.RemoveAt(3);
      Console.Write( "\nAfter First Removed ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      
      // remove an element at index 4
      arrayList.RemoveAt(4);
      Console.Write( "\nAfter Second Removed: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Initial ArrayList: A 1 2 4 B C D 
After First Removed A 1 2 B C D 
After Second Removed: A 1 2 B D 

Example 4: Remove Person Details from ArrayList

Here is an example, creates a Person class, store multiple Person objects in an ArrayList, and use the RemoveAt() method to remove the element at the second index −

using System;
using System.Collections;
class Person
{
   public string Name { get; set; }
   public int Age { get; set; }

   public Person(string name, int age)
   {
      Name = name;
      Age = age;
   }

   public override string ToString()
   {
      return $"Name: {Name}, Age: {Age}";
   }
}

class Program
{
   static void Main()
   {
      // Create an ArrayList to store Person objects
      ArrayList people = new ArrayList()
      {
         new Person("Aman", 26),
         new Person("Kumar", 25),
         new Person("Gupta", 24),
         new Person("Akash", 25)
      };

      Console.WriteLine("Original List of People:");
      foreach (Person person in people)
         Console.WriteLine(person);

      // Remove the element at the second index
      Console.WriteLine("\nRemoving the person at index 2 (Gupta)...");
      people.RemoveAt(2);

      Console.WriteLine("\nUpdated List of People:");
      foreach (Person person in people)
         Console.WriteLine(person);
   }
}

Output

Following is the output −

Original List of People:
Name: Aman, Age: 26
Name: Kumar, Age: 25
Name: Gupta, Age: 24
Name: Akash, Age: 25

Removing the person at index 2 (Gupta)...

Updated List of People:
Name: Aman, Age: 26
Name: Kumar, Age: 25
Name: Akash, Age: 25
csharp_arraylist.htm
Advertisements