Remove the first occurrence from the StringCollection in C#

The StringCollection class in C# provides the Remove() method to remove the first occurrence of a specified string from the collection. This method is useful when you need to eliminate duplicate values or specific elements from your string collection.

Syntax

Following is the syntax for removing the first occurrence from a StringCollection −

stringCollection.Remove(string value);

Parameters

  • value − The string to remove from the StringCollection. The method removes only the first occurrence if multiple instances exist.

Return Value

The Remove() does not return a value. It modifies the original StringCollection by removing the first matching element.

Using Remove() with Duplicate Elements

When a StringCollection contains duplicate values, the Remove() method removes only the first occurrence found −

using System;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      StringCollection stringCol = new StringCollection();
      String[] arr = new String[] { "100", "200", "100", "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);
      
      // Remove first occurrence of "100"
      stringCol.Remove("100");
      Console.WriteLine("After removing first '100':");
      Console.WriteLine("Total number of elements now = " + stringCol.Count);
      
      Console.WriteLine("Remaining elements:");
      foreach (string res in stringCol) {
         Console.WriteLine(res);
      }
   }
}

The output of the above code is −

Array elements...
100
200
100
400
500
Total number of elements = 5
After removing first '100':
Total number of elements now = 4
Remaining elements:
200
100
400
500

Using Remove() for Multiple Deletions

You can call the Remove() method multiple times to remove different elements from the collection −

using System;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      StringCollection stringCol = new StringCollection();
      String[] arr = new String[] { "10", "20", "30", "40", "50", "60", "70", "80" };
      
      Console.WriteLine("Array elements...");
      foreach (string res in arr) {
         Console.WriteLine(res);
      }
      
      stringCol.AddRange(arr);
      Console.WriteLine("Total number of elements = " + stringCol.Count);
      
      // Remove multiple elements
      stringCol.Remove("50");
      stringCol.Remove("60");
      stringCol.Remove("70");
      
      Console.WriteLine("After removing '50', '60', '70':");
      Console.WriteLine("Total number of elements now = " + stringCol.Count);
      
      Console.WriteLine("Remaining elements:");
      foreach (string res in stringCol) {
         Console.WriteLine(res);
      }
   }
}

The output of the above code is −

Array elements...
10
20
30
40
50
60
70
80
Total number of elements = 8
After removing '50', '60', '70':
Total number of elements now = 5
Remaining elements:
10
20
30
40
80

Key Rules

  • The Remove() method removes only the first occurrence of the specified string.

  • If the string is not found in the collection, no exception is thrown and the collection remains unchanged.

  • The method performs a case-sensitive comparison when searching for the string to remove.

  • After removal, the indices of subsequent elements are automatically adjusted.

Conclusion

The Remove() method in StringCollection provides an efficient way to remove the first occurrence of a specified string. It automatically handles index management and performs case-sensitive matching, making it ideal for cleaning up string collections with duplicate or unwanted values.

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

157 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements