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
How to remove element from the specified index of the List in C#?
To remove an element from a specified index in a List in C#, you use the RemoveAt() method. This method removes the element at the given index and automatically shifts all subsequent elements one position to the left.
Syntax
Following is the syntax for the RemoveAt() method −
list.RemoveAt(int index);
Parameters
index − The zero-based index of the element to remove. Must be a valid index (0 ? index < Count).
Using RemoveAt() with String List
The following example demonstrates removing an element at index 5 from a list of strings −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<string> list = new List<string>();
list.Add("Ryan");
list.Add("Kevin");
list.Add("Andre");
list.Add("Tom");
list.Add("Fred");
list.Add("Jason");
list.Add("Jacob");
list.Add("David");
Console.WriteLine("Count of elements in the List = " + list.Count);
Console.WriteLine("Enumerator iterates through the list elements...");
List<string>.Enumerator demoEnum = list.GetEnumerator();
while (demoEnum.MoveNext()) {
string res = demoEnum.Current;
Console.WriteLine(res);
}
list.RemoveAt(5);
Console.WriteLine("\nCount of elements in the List [UPDATED] = " + list.Count);
Console.WriteLine("Enumerator iterates through the list elements...[UPDATED]");
demoEnum = list.GetEnumerator();
while (demoEnum.MoveNext()) {
string res = demoEnum.Current;
Console.WriteLine(res);
}
}
}
The output of the above code is −
Count of elements in the List = 8 Enumerator iterates through the list elements... Ryan Kevin Andre Tom Fred Jason Jacob David Count of elements in the List [UPDATED] = 7 Enumerator iterates through the list elements...[UPDATED] Ryan Kevin Andre Tom Fred Jacob David
Using RemoveAt() with Integer List
The following example shows removing an element at index 2 from a list of integers −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(String[] args) {
List<int> list = new List<int>();
list.Add(25);
list.Add(50);
list.Add(75);
list.Add(100);
list.Add(200);
Console.WriteLine("Original List:");
foreach(int item in list) {
Console.WriteLine(item);
}
Console.WriteLine("Count of elements in the List = " + list.Count);
list.RemoveAt(2);
Console.WriteLine("\nUpdated List after removing element at index 2:");
foreach(int item in list) {
Console.WriteLine(item);
}
Console.WriteLine("Count of elements in the List [UPDATED] = " + list.Count);
}
}
The output of the above code is −
Original List: 25 50 75 100 200 Count of elements in the List = 5 Updated List after removing element at index 2: 25 50 100 200 Count of elements in the List [UPDATED] = 4
How It Works
When you call RemoveAt(index), the method performs the following operations:
Removes the element at the specified index
Shifts all elements after the removed index one position to the left
Decreases the
Countproperty by 1Throws
ArgumentOutOfRangeExceptionif the index is invalid
Key Points
The
RemoveAt()method has O(n) time complexity because it needs to shift elementsRemoving from the end of the list is faster than removing from the beginning
Always ensure the index is valid (0 ? index < list.Count) to avoid exceptions
Conclusion
The RemoveAt() method in C# provides an efficient way to remove elements from a List at a specific index. It automatically handles the shifting of subsequent elements and updates the list count, making it the preferred method for index-based element removal.
