Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Remove element at specified index of Collection in C#
To remove an element at a specified index from a Collection in C#, you use the RemoveAt() method. This method removes the element at the specified zero-based index and shifts all subsequent elements down by one position.
Syntax
Following is the syntax for the RemoveAt() method −
collection.RemoveAt(index);
Parameters
index − The zero-based index of the element to remove. Must be a valid index within the collection bounds.
Using RemoveAt() for Single Element Removal
The following example demonstrates removing a single element at index 3 from a Collection −
using System;
using System.Collections.ObjectModel;
public class Demo {
public static void Main() {
Collection<string> col = new Collection<string>();
col.Add("Andy");
col.Add("Kevin");
col.Add("John");
col.Add("Kevin");
col.Add("Mary");
col.Add("Katie");
col.Add("Barry");
col.Add("Nathan");
col.Add("Mark");
Console.WriteLine("Count of elements = " + col.Count);
Console.WriteLine("Iterating through the collection...");
var enumerator = col.GetEnumerator();
while (enumerator.MoveNext()) {
Console.WriteLine(enumerator.Current);
}
col.RemoveAt(3);
Console.WriteLine("Count of elements (updated) = " + col.Count);
Console.WriteLine("Iterating through the collection... (updated)");
enumerator = col.GetEnumerator();
while (enumerator.MoveNext()) {
Console.WriteLine(enumerator.Current);
}
}
}
The output of the above code is −
Count of elements = 9 Iterating through the collection... Andy Kevin John Kevin Mary Katie Barry Nathan Mark Count of elements (updated) = 8 Iterating through the collection... (updated) Andy Kevin John Mary Katie Barry Nathan Mark
Using RemoveAt() for Multiple Element Removal
When removing multiple elements, be aware that indices shift after each removal. The following example removes multiple elements −
using System;
using System.Collections.ObjectModel;
public class Demo {
public static void Main() {
Collection<string> col = new Collection<string>();
col.Add("One");
col.Add("Two");
col.Add("Three");
col.Add("Four");
col.Add("Five");
col.Add("Six");
Console.WriteLine("Count of elements = " + col.Count);
Console.WriteLine("Iterating through the collection...");
var enumerator = col.GetEnumerator();
while (enumerator.MoveNext()) {
Console.WriteLine(enumerator.Current);
}
col.RemoveAt(1); // Removes "Two"
col.RemoveAt(2); // Removes "Four" (was at index 3, now at 2)
col.RemoveAt(3); // Removes "Six" (was at index 5, now at 3)
Console.WriteLine("Count of elements (updated) = " + col.Count);
Console.WriteLine("Iterating through the collection... (updated)");
enumerator = col.GetEnumerator();
while (enumerator.MoveNext()) {
Console.WriteLine(enumerator.Current);
}
}
}
The output of the above code is −
Count of elements = 6 Iterating through the collection... One Two Three Four Five Six Count of elements (updated) = 3 Iterating through the collection... (updated) One Three Five
Key Rules
The index must be within the valid range (0 to Count-1), otherwise an
ArgumentOutOfRangeExceptionis thrown.After removing an element, all subsequent elements shift down by one position.
The Count property is automatically decremented after each removal.
When removing multiple elements in sequence, consider removing from higher indices first to avoid index shifting issues.
Conclusion
The RemoveAt() method provides an efficient way to remove elements from a Collection at specific indices. Remember that removing elements causes subsequent elements to shift positions, which affects their indices for future operations.
