C# | Remove from the specified index of the StringCollection

StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace.
StringCollection.RemoveAt(Int32) method is used to remove the string at the specified index of the StringCollection.

Syntax:

public void RemoveAt (int index);

Here, index is the zero-based index of the string to remove.



Exception: This method will give ArgumentOutOfRangeException if the index is less than zero or index is equal to or greater than Count.

Note: This method is an O(n) operation, where n is Count.

Below programs illustrate the use of StringCollection.RemoveAt(Int32) Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to remove the string at the
// specified index of the StringCollection
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // creating a StringCollection named myCol
        StringCollection myCol = new StringCollection();
  
        // creating a string array named myArr
        String[] myArr = new String[] { "A", "B", "C", "D", "E" };
  
        // Copying the elements of a string
        // array to the end of the StringCollection.
        myCol.AddRange(myArr);
  
        Console.WriteLine("Elements in StringCollection myCol are : ");
  
        // Displaying elements in StringCollection
        // named myCol
        foreach(Object obj in myCol)
            Console.WriteLine(obj);
  
        // Removing element at index 3
        myCol.RemoveAt(3);
  
        Console.WriteLine("Elements in StringCollection myCol are : ");
  
        // Displaying elements in StringCollection
        // named myCol
        foreach(Object obj in myCol)
            Console.WriteLine(obj);
    }
}

chevron_right


Output:

Elements in StringCollection myCol are : 
A
B
C
D
E
Elements in StringCollection myCol are : 
A
B
C
E

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to remove the string at the
// specified index of the StringCollection
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // creating a StringCollection named myCol
        StringCollection myCol = new StringCollection();
  
        // creating a string array named myArr
        String[] myArr = new String[] { "1", "2", "3", "4", "5" };
  
        // Copying the elements of a string
        // array to the end of the StringCollection.
        myCol.AddRange(myArr);
  
        Console.WriteLine("Elements in StringCollection myCol are : ");
  
        // Displaying elements in StringCollection
        // named myCol
        foreach(Object obj in myCol)
            Console.WriteLine(obj);
  
        // Removing element at index -5
        // It should raise "ArgumentOutOfRangeException"
        // as index is less than 0
        myCol.RemoveAt(-5);
  
        Console.WriteLine("Elements in StringCollection myCol are : ");
  
        // Displaying elements in StringCollection
        // named myCol
        foreach(Object obj in myCol)
            Console.WriteLine(obj);
    }
}

chevron_right


Output:

Unhandled Exception:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Reference:



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.