Remove from the specified index of the StringCollection in C#

The StringCollection class in C# provides the RemoveAt() method to remove an element from a specified index. This method removes the string at the given index and shifts all subsequent elements one position to the left.

Syntax

Following is the syntax for the RemoveAt() method −

stringCollection.RemoveAt(int index);

Parameters

  • index − The zero-based index of the element to remove from the StringCollection.

Using RemoveAt() to Remove Single Element

The following example demonstrates removing an element from a specific index in a StringCollection −

using System;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      StringCollection stringCol = new StringCollection();
      String[] arr = new String[] { "100", "200", "300", "400", "500" };
      Console.WriteLine("Array elements...");
      foreach (string res in arr) {
         Console.WriteLine(res);
      }
      stringCol.AddRange(arr);
      Console.WriteLine("Total number of elements = " + stringCol.Count);
      
      Console.WriteLine("Removing element at index 3...");
      stringCol.RemoveAt(3);
      Console.WriteLine("Total number of elements now = " + stringCol.Count);
      
      Console.WriteLine("Remaining elements:");
      foreach (string element in stringCol) {
         Console.WriteLine(element);
      }
   }
}

The output of the above code is −

Array elements...
100
200
300
400
500
Total number of elements = 5
Removing element at index 3...
Total number of elements now = 4
Remaining elements:
100
200
300
500

Using RemoveAt() to Remove Multiple Elements

When removing multiple elements, be careful about index shifting. Each removal shifts subsequent elements to the left −

using System;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      StringCollection stringCol = new StringCollection();
      String[] arr = new String[] { "A", "B", "C", "D", "E" };
      Console.WriteLine("Original elements...");
      foreach (string res in arr) {
         Console.WriteLine(res);
      }
      stringCol.AddRange(arr);
      Console.WriteLine("Total number of elements = " + stringCol.Count);
      
      Console.WriteLine("Removing element at index 1 (B)...");
      stringCol.RemoveAt(1);
      Console.WriteLine("Removing element at index 2 (now D)...");
      stringCol.RemoveAt(2);
      Console.WriteLine("Total number of elements now = " + stringCol.Count);
      
      Console.WriteLine("Remaining elements:");
      foreach (string element in stringCol) {
         Console.WriteLine(element);
      }
   }
}

The output of the above code is −

Original elements...
A
B
C
D
E
Total number of elements = 5
Removing element at index 1 (B)...
Removing element at index 2 (now D)...
Total number of elements now = 3
Remaining elements:
A
C
E

How It Works

When RemoveAt() is called, the following occurs:

  • The element at the specified index is removed from the collection.

  • All elements after the removed index shift one position to the left.

  • The Count property decreases by one.

  • If the index is out of range, an ArgumentOutOfRangeException is thrown.

RemoveAt() Index Shifting Before: RemoveAt(2) A 0 B 1 C 2 D 3 E 4 After: Element removed, indices shift A 0 B 1 D 2 E 3 ?

Conclusion

The RemoveAt() method provides an efficient way to remove elements from a StringCollection by index. Remember that removing elements shifts subsequent indices, which is important when removing multiple elements in sequence.

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

196 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements