How to remove item from list in C?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In C#, removing an item from List<T> can mean a few different things: remove by value, remove by index, remove every matching item, or remove a range. The right method depends on how you identify the element and whether you are deleting one item or many. Understanding the differences helps you avoid off-by-one bugs, invalid indexes, and collection-modification errors.
Remove by value with Remove
Use Remove when you know the item value and want to delete the first matching element.
This removes only the first "Grace". The method returns true if something was removed and false if the value was not found. That return value is useful when the item may or may not exist.
Remove by index with RemoveAt
Use RemoveAt when you know the position rather than the value.
This deletes the element at index 2, which is 30. Remember that List<T> uses zero-based indexing. If the index is outside the valid range, RemoveAt throws an exception.
Remove multiple items with RemoveAll
If you need to remove every item that matches a condition, RemoveAll is the cleanest option.
This removes all even numbers and returns how many items were deleted. It is much safer than looping forward and calling RemoveAt repeatedly because the list is shrinking underneath the loop.
Remove a block with RemoveRange
When the items form a continuous slice, RemoveRange is more expressive than removing them one by one.
This removes three items starting at index 1, so the result is "a, e". It is a good fit when you know the starting index and how many items to drop.
Working with custom objects
For custom types, Remove uses the type's equality behavior. If two objects should be treated as the same logical item, implement Equals and GetHashCode, or remove by predicate with RemoveAll.
That predicate-based approach is often clearer than relying on object identity.
Common Pitfalls
The most common mistake is removing items inside a foreach loop. That throws an exception because the collection is being modified during enumeration. If you need selective deletion, use RemoveAll, build a new list, or iterate backward with a traditional for loop.
Another issue is confusing value removal with index removal. Remove(2) tries to remove the value 2, while RemoveAt(2) removes the third element.
Index handling is another frequent bug. After you remove one item, all later items shift left. If you are looping forward and deleting as you go, you can skip items accidentally.
Finally, remember that removing from a list changes Count but not necessarily Capacity. That usually does not matter, but it explains why a list may still hold allocated memory after elements are removed.
Summary
- Use
Removeto delete the first matching value. - Use
RemoveAtto delete the item at a known index. - Use
RemoveAllto delete every item matching a condition. - Use
RemoveRangefor a continuous block of items. - Avoid removing items inside
foreach; prefer predicate-based removal or a backward loop.

