C#
list
remove item
programming
tutorial

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.

csharp
1using System;
2using System.Collections.Generic;
3
4class Program
5{
6    static void Main()
7    {
8        var names = new List<string> { "Ada", "Grace", "Linus", "Grace" };
9
10        bool removed = names.Remove("Grace");
11
12        Console.WriteLine(removed);
13        Console.WriteLine(string.Join(", ", names));
14    }
15}

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.

csharp
1using System;
2using System.Collections.Generic;
3
4class Program
5{
6    static void Main()
7    {
8        var scores = new List<int> { 10, 20, 30, 40 };
9        scores.RemoveAt(2);
10
11        Console.WriteLine(string.Join(", ", scores));
12    }
13}

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.

csharp
1using System;
2using System.Collections.Generic;
3
4class Program
5{
6    static void Main()
7    {
8        var numbers = new List<int> { 1, 2, 3, 4, 5, 6 };
9        int removedCount = numbers.RemoveAll(n => n % 2 == 0);
10
11        Console.WriteLine("Removed: " + removedCount);
12        Console.WriteLine(string.Join(", ", numbers));
13    }
14}

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.

csharp
1using System;
2using System.Collections.Generic;
3
4class Program
5{
6    static void Main()
7    {
8        var letters = new List<string> { "a", "b", "c", "d", "e" };
9        letters.RemoveRange(1, 3);
10
11        Console.WriteLine(string.Join(", ", letters));
12    }
13}

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.

csharp
1var users = new List<User>
2{
3    new User { Id = 1, Name = "Ada" },
4    new User { Id = 2, Name = "Grace" }
5};
6
7users.RemoveAll(u => u.Id == 2);

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 Remove to delete the first matching value.
  • Use RemoveAt to delete the item at a known index.
  • Use RemoveAll to delete every item matching a condition.
  • Use RemoveRange for a continuous block of items.
  • Avoid removing items inside foreach; prefer predicate-based removal or a backward loop.

Course illustration
Course illustration

All Rights Reserved.