C# ArrayList - Remove() Method



The C# ArrayList Remove() method is used to remove or omit the first occurrence of the specific object/element from the ArrayList.

If the ArrayList does not contain the specified object, it remains unchanged, and no exception is thrown. This method Performs the linear search operation; therefore, this method takes O(n) time complexity, where n is count.

Syntax

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

public virtual void Remove (object? obj);

Parameters

This method accepts a single parameter −

  • value: It is an element that will be removed from the ArrayList. It can be null.

Return value

This method does not return any value.

Example 1: Remove an Integer from ArrayList

Following is the basic example of the Remove() method to remove an element from the ArrayList −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { 1, 2, 2, 3, 4 };
      
      Console.Write("Before Remove: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      
      // remove first occurence of 2
      arrayList.Remove(2);
      
      Console.Write( "\nAfter Remove: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Before Remove: 1 2 3 4 
After Remove: 1 2 3 

Example 2: Remove a First Occurrence of an Object

Let's see another example where we remove first occurrence of an element from ArrayList using the Remove() method −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { 1, 2, 2, 3, 4 };
      
      Console.Write("Before Remove: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      
      // remove first occurence of 2
      arrayList.Remove(2);
      
      Console.Write( "\nAfter Remove: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Before Remove: 1 2 2 3 4 
After Remove: 1 2 3 4 

Example 3: Remove Both Integer and Character from ArrayList

The below example creates an ArrayList with some elements. We then use the Remove() 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("Before Remove: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      
      // remove a character
      arrayList.Remove("A");
      Console.Write( "\nAfter Remove a character: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      
      // remove an integer
      arrayList.Remove(2);
      Console.Write( "\nAfter Remove an integer: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
   }
}

Output

Following is the output −

Before Remove: A 1 2 4 B C D 
After Remove a character: 1 2 4 B C D 
After Remove an integer: 1 4 B C D

Example 4: Remove Attendee from ArrayList

This example shows how to track who is coming to an event and how to remove a specific attendee who dropped their registration −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      // Create an ArrayList to store event attendees
      ArrayList attendees = new ArrayList()
      {
         "Akash",
         "Aman",
         "Rahul",
         "Sunny",
         "Aman"
      };

      Console.WriteLine("Initial Attendee List:");
      foreach (var attendee in attendees)
          Console.Write(attendee + " ");

      // Remove the first occurrence of "Aman" 
      // (if he cancels his registration)
      Console.WriteLine("\nRemoving attendee: Aman");
      attendees.Remove("Aman");

      Console.WriteLine("\nUpdated Attendee List:");
      foreach (var attendee in attendees)
         Console.Write(attendee + " ");

      // Attempt to remove someone not in the list
      Console.WriteLine("\nAttempting to remove attendee: Vivek");
      attendees.Remove("Vivek");

      Console.WriteLine("\nFinal Attendee List:");
      foreach (var attendee in attendees)
         Console.Write(attendee + " ");
   }
}

Output

Following is the output −

Initial Attendee List:
Akash Aman Rahul Sunny Aman 
Removing attendee: Aman

Updated Attendee List:
Akash Rahul Sunny Aman 
Attempting to remove attendee: Vivek

Final Attendee List:
Akash Rahul Sunny Aman 
csharp_arraylist.htm
Advertisements