In C#, List.RemoveAt(index) deletes the item at one position. The index is zero-based, so 0 is the first item and Count - 1 is the last.
All items after that position slide one place to the left, and Count drops by 1. Use it when you know where an item is. When you know what the item is, Remove() is the better choice.
This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.RemoveAt(...). A List<T> is a class, so two variables can share one list. It keeps items in the order you put them, allows duplicates, and grows when needed. An array, by comparison, has a fixed length.
Syntax
Pass the index of the item to delete. The valid range is 0 to Count - 1.
Removes the first item. Everything else moves left by one.
names.RemoveAt(names.Count - 1)
Removes the last item. Nothing needs to shift, so it is fast.
names.RemoveAt(names.Count)
Throws ArgumentOutOfRangeException. There is no item at index Count.
RemoveAt is O(n) in general because of the shift, but removing the last item is O(1). A negative index or an index of Count or more throws ArgumentOutOfRangeException. The method returns void, so read the item first if you still need it: var item = list[i]; list.RemoveAt(i);.
Library declarations
C#
public void RemoveAt(int index);
Parameters
Parameter
Description
index
Required. The zero-based position of the item to delete. It must be from 0 to Count - 1.
Return Value
Return Value
Description
void
Nothing is returned. The item is gone and Count is one less. The removed value is not handed back.
C# Examples
Example 1 — Remove the Second Item
Delete the item at index 1 and watch the later item slide into its place.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> names = new List<string> { "Asha", "Ben", "Chen" };
names.RemoveAt(1);
Console.WriteLine(string.Join(", ", names));
Console.WriteLine(names.Count);
Console.WriteLine(names[1]);
}
}
Output
Asha, Chen
2
Chen
How It Works
Position 1. Index 0 is "Asha" and index 1 is "Ben", so "Ben" is the item that is deleted.
Slide left."Chen" was at index 2 and moves to index 1. The list has no hole.
Smaller count. Three items minus one leaves Count equal to 2.
Same index, new item.names[1] used to be "Ben". Now it reads "Chen".
Example 2 — The Last Valid Index
Remove the last item with Count - 1, then see what happens with an index equal to Count.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> list = new List<int> { 10, 20, 30 };
list.RemoveAt(list.Count - 1);
Console.WriteLine(string.Join(", ", list));
try {
list.RemoveAt(list.Count);
} catch (ArgumentOutOfRangeException) {
Console.WriteLine("no item at index " + list.Count);
}
Console.WriteLine(list.Count);
}
}
Output
10, 20
no item at index 2
2
How It Works
Last item. With 3 items, Count - 1 is 2. RemoveAt(2) deletes 30, and nothing else has to move.
Count is off the end. Now Count is 2, and the valid indexes are only 0 and 1. RemoveAt(2) asks for a slot that does not exist.
Exception is caught.ArgumentOutOfRangeException is thrown, the catch block prints a message, and the program goes on.
List untouched. The failed call removed nothing, so Count is still 2.
Example 3 — Remove Even Numbers in a Loop
Go from the end to the start so that each removal does not disturb the indexes you have not visited yet.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };
for (int i = numbers.Count - 1; i >= 0; i--) {
if (numbers[i] % 2 == 0) {
numbers.RemoveAt(i);
}
}
Console.WriteLine(string.Join(", ", numbers));
}
}
Output
1, 3, 5
How It Works
Start at the end.i begins at Count - 1 and counts down to 0, so the loop reads the list from right to left.
Test then remove. When numbers[i] is even, RemoveAt(i) deletes it. Only items to the right of i shift, and those were already checked.
Why not forwards. In a forward loop, deleting index 1 moves the next item into index 1, and the loop then jumps past it. Going backwards avoids that.
Shorter way.numbers.RemoveAll(n => n % 2 == 0) does the same job in one line, with the loop written for you.
Frequently Asked Questions
It deletes the item at the index you pass and moves every later item one place to the left. Count goes down by 1.
RemoveAt throws ArgumentOutOfRangeException when the index is negative or equal to or greater than Count. The list is not changed.
No, it returns void. Read the item with list[index] before you call RemoveAt if you still need it.
Call list.RemoveAt(list.Count - 1). Check that Count is above 0 first, because an empty list has no last item.
Use a for loop that counts down from Count - 1 to 0, or call RemoveAll with a predicate. A foreach over the same list throws InvalidOperationException when the list changes.