Here are the different ways to remove items from a list:
- Use List.RemoveAt() to remove an item by index.
- Use List.Remove() to remove a specific item.
- Use List.RemoveAll() to remove all items based on a condition.
- Use List.RemoveRange() to remove N items starting at an index.
- Use List.Clear() to remove all items.
I’ll show examples of using these methods.
Remove item by index with List.RemoveAt()
You can use List.RemoveAt() to remove an item at an index (0-based). Here’s an example of removing the first and last item from the list:
var list = new List<string>()
{
"A", "B", "C", "D"
};
//Remove first item ("A"):
list.RemoveAt(0);
//Remove last item ("D"):
list.RemoveAt(list.Count - 1);
Console.WriteLine(string.Join(",", list));
Code language: C# (cs)
This removes the first item “A” and the last item “D” and outputs the list’s contents:
B,C
Code language: plaintext (plaintext)
Internally, the List<T> class stores items in a dynamically-sized array. So when you remove an item by index, it actually shifts all items starting at index + 1 to the left by 1 (using Array.Copy()).
Remove an item with List.Remove()
You can use List.Remove() to remove a specific item from the list. Here’s an example:
var list = new List<string>()
{
"A", "B", "C"
};
list.Remove("B");
Console.WriteLine(string.Join(",", list));
Code language: C# (cs)
This outputs the list contents with “B” removed:
A,C
Code language: plaintext (plaintext)
List.Remove() internally searches for the index of the item to remove, then uses List.RemoveAt() to actually remove the item.
Removes only the first occurrence
If an item is repeated in the list, List.Remove() only removes the first occurrence. Here’s an example:
var intList = new List<int>()
{
1, 2, 1
};
intList.Remove(1);
Console.WriteLine(string.Join(",", intList));
Code language: C# (cs)
This outputs the lists contents with the first 1 removed:
2,1
Code language: plaintext (plaintext)
Remove a complex object
List.Remove() looks for the item to remove by using the type’s default IEqualityComparer (i.e. the Equals() method). If you haven’t implemented this for your type, it’ll remove the item by reference. Here’s an example:
var bob = new Person() { Name = "Bob" };
var linda = new Person() { Name = "Linda" };
var teddy = new Person() { Name = "Teddy" };
var list = new List<Person>()
{
bob,
linda,
teddy
};
list.Remove(bob);
Console.WriteLine(string.Join(",", list.Select(p => p.Name)));
Code language: C# (cs)
This removes the bob item from the list and outputs:
Linda,Teddy
Code language: plaintext (plaintext)
This is usually the behavior you want. However, sometimes you may want to be able to remove a complex object based on the value of one or more properties. You can either implement Equals() in your type or use RemoveAll(), like this:
list.RemoveAll(p => p.Name == "Bob");
Code language: C# (cs)
Remove items based on a condition with List.RemoveAll()
The simplest way to remove items based on a condition is by using List.RemoveAll(). You specify the condition by passing in a lambda. Here’s an example:
var list = new List<int>()
{
1,2,3,4,5
};
int removedCount = list.RemoveAll(i => i < 3);
Console.WriteLine($"Removed count: {removedCount}");
Console.WriteLine(string.Join(",", list));
Code language: C# (cs)
This removes all items that are less than 3 and outputs the following:
Removed count: 2
3,4,5
Code language: plaintext (plaintext)
Internally, List.RemoveAll() loops over all items and shifts the items to keep to the left. Unlike all the other removal methods, this copies one item at a time (instead of bulk copying with Array.Copy()). This is a nice, concise alternative to removing items manually by looping backward and using List.RemoveAt().
Remove a range of items with List.RemoveRange()
Use List.RemoveRange() to remove a range of items based on their position in the list. You pass in the starting index and the number of items to remove. Here’s an example:
var list = new List<string>()
{
"A", "B", "C", "D"
};
//Removes the first two items
list.RemoveRange(index: 0, count: 2);
Console.WriteLine(string.Join(",", list));
Code language: C# (cs)
This removes 2 items starting at index 0 (A and B) and outputs the list’s contents:
C,D
Code language: plaintext (plaintext)
Internally, List.RemoveRange() shifts items starting at index + count to the left using Array.Copy().
Remove all items with List.Clear()
If you want to keep the list object but remove all items from it, use List.Clear():
var list = new List<int>()
{
1,2,3,4,5
};
Console.WriteLine($"Count before clear = {list.Count}");
list.Clear();
Console.WriteLine($"Count after clear = {list.Count}");
Code language: C# (cs)
This outputs the following:
Count before clear = 5
Count after clear = 0
Code language: plaintext (plaintext)