Collection<T>.RemoveAt(Int32) is used to remove the element at the specified index of the Collection<T>.
Syntax:
public void RemoveAt (int index);
Here, index is the zero-based index of the element to remove.
Exception: This method will give ArgumentOutOfRangeException if the index is less than zero OR index is equal to or greater than Count.
Below given are some examples to understand the implementation in a better way:
Example 1:
// C# code to remove the element // at the specified index of the Collection using System; using System.Collections.Generic; using System.Collections.ObjectModel; class GFG { // Driver code public static void Main() { // Creating a collection of strings Collection<string> myColl = new Collection<string>(); // Adding elements in Collection myColl myColl.Add("A"); myColl.Add("B"); myColl.Add("C"); myColl.Add("D"); myColl.Add("E"); // To print the count of elements in Collection Console.WriteLine("Count : " + myColl.Count); // Displaying the elements in myColl foreach(string str in myColl) { Console.WriteLine(str); } // Removing the element at the // specified index of the Collection myColl.RemoveAt(2); // To print the count of elements in Collection Console.WriteLine("Count : " + myColl.Count); // Displaying the elements in myColl foreach(string str in myColl) { Console.WriteLine(str); } } } |
Output:
Count : 5 A B C D E Count : 4 A B D E
Example 2:
// C# code to remove the element // at the specified index of the Collection using System; using System.Collections.Generic; using System.Collections.ObjectModel; class GFG { // Driver code public static void Main() { // Creating a collection of ints Collection<int> myColl = new Collection<int>(); // Adding elements in Collection myColl myColl.Add(2); myColl.Add(3); myColl.Add(4); myColl.Add(5); // To print the count of elements in Collection Console.WriteLine("Count : " + myColl.Count); // Displaying the elements in myColl foreach(int i in myColl) { Console.WriteLine(i); } // Removing the element at the // specified index of the Collection // This should raise "ArgumentOutOfRangeException" // as the index is less than 0 myColl.RemoveAt(-4); // To print the count of elements in Collection Console.WriteLine("Count : " + myColl.Count); // Displaying the elements in myColl foreach(int i in myColl) { Console.WriteLine(i); } } } |
Runtime Error:
Unhandled Exception:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Note: This method is an O(n) operation, where n is Count.
Reference:
Recommended Posts:
- C# | Remove the element at the specified index of the ArrayList
- C# | How to remove the element from the specified index of the List
- Creating an Index From the Specified Index at the Start of a Collection in C#
- Creating an Index From the End of a Collection at a Specified Index Position in C#
- C# | Remove from the specified index of the StringCollection
- C# | Remove from the specified index of a SortedList
- C# | Remove the entry at specified index from OrderedDictionary
- C# | Insert an element into the ArrayList at the specified index
- C# | Gets or Sets the element at the specified index in the List
- C# | Insert an element into Collection<T> at specified index
- C# | Get or set the element at specified index in Collection<T>
- C# | Get or set the element at the specified index in ArrayList
- C# | Gets or sets the element at the specified index in StringCollection
- Finding the Index of First Element of the Specified Sequence in C#
- C# | Remove the element with the specified key from the Hashtable
- C# | Remove the specified element from a HashSet
- C# | Remove the element with the specified key from a SortedList
- C# | Get or Set at specified index in StringCollection
- C# | Copy StringCollection at the specified index of array
- C# | How to insert the elements of a collection into the List at the specified index
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.

