Remove a range of elements from the ArrayList in C#

The ArrayList class in C# provides the RemoveRange() method to remove a range of elements from the collection. This method is useful when you need to delete multiple consecutive elements efficiently in a single operation.

Syntax

Following is the syntax for the RemoveRange() method −

public virtual void RemoveRange(int index, int count)

Parameters

  • index − The zero-based starting index of the range of elements to remove.

  • count − The number of elements to remove.

RemoveRange(index, count) Visual A B C D E F G H 0 1 2 3 4 5 6 7 RemoveRange(3, 2) Removes elements D and E

Using RemoveRange() to Remove Consecutive Elements

Example

using System;
using System.Collections;

public class Demo {
   public static void Main(String[] args) {
      ArrayList list1 = new ArrayList();
      list1.Add("A");
      list1.Add("B");
      list1.Add("C");
      list1.Add("D");
      list1.Add("E");
      list1.Add("F");
      list1.Add("G");
      list1.Add("H");
      list1.Add("I");
      
      Console.WriteLine("Elements in ArrayList1...");
      foreach (string res in list1) {
         Console.WriteLine(res);
      }
      
      ArrayList list2 = new ArrayList();
      list2.Add("A");
      list2.Add("B");
      list2.Add("C");
      list2.Add("D");
      list2.Add("E");
      list2.Add("F");
      list2.Add("G");
      list2.Add("H");
      list2.Add("I");
      
      Console.WriteLine("Elements in ArrayList2...");
      foreach (string res in list2) {
         Console.WriteLine(res);
      }
      
      Console.WriteLine("Is ArrayList1 equal to ArrayList2? = " + list1.Equals(list2));
      list2.RemoveRange(3, 2);
      
      Console.WriteLine("Elements in ArrayList2 after removing elements in a range...");
      foreach (string res in list2) {
         Console.WriteLine(res);
      }
   }
}

The output of the above code is −

Elements in ArrayList1...
A
B
C
D
E
F
G
H
I
Elements in ArrayList2...
A
B
C
D
E
F
G
H
I
Is ArrayList1 equal to ArrayList2? = False
Elements in ArrayList2 after removing elements in a range...
A
B
C
F
G
H
I

Using RemoveRange() to Remove All Elements from a Position

Example

using System;
using System.Collections;

public class Demo {
   public static void Main(String[] args) {
      ArrayList list1 = new ArrayList();
      list1.Add("A");
      list1.Add("B");
      list1.Add("C");
      list1.Add("D");
      list1.Add("E");
      list1.Add("F");
      list1.Add("G");
      list1.Add("H");
      list1.Add("I");
      
      Console.WriteLine("Elements in ArrayList1...");
      foreach (string res in list1) {
         Console.WriteLine(res);
      }
      
      ArrayList list2 = new ArrayList();
      list2.Add("One");
      list2.Add("Two");
      list2.Add("Three");
      list2.Add("Four");
      list2.Add("Five");
      list2.Add("Six");
      list2.Add("Seven");
      
      Console.WriteLine("Elements in ArrayList2...");
      foreach (string res in list2) {
         Console.WriteLine(res);
      }
      
      list2.RemoveRange(1, 6);
      
      Console.WriteLine("Elements in ArrayList2 after removing elements in a range...");
      foreach (string res in list2) {
         Console.WriteLine(res);
      }
   }
}

The output of the above code is −

Elements in ArrayList1...
A
B
C
D
E
F
G
H
I
Elements in ArrayList2...
One
Two
Three
Four
Five
Six
Seven
Elements in ArrayList2 after removing elements in a range...
One

Conclusion

The RemoveRange() method provides an efficient way to remove multiple consecutive elements from an ArrayList. By specifying the starting index and count, you can remove any range of elements in a single operation, which is more efficient than removing elements individually.

Updated on: 2026-03-17T07:04:36+05:30

192 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements