In C#, List.Remove(item) looks through the list from the start and deletes the first item that equals the value you pass. Every item after it moves one place to the left, so there is no gap.
It returns a bool: true if it found and removed an item, false if the value was not in the list. A missing value is not an error. To remove by position instead of by value, use RemoveAt().
This is an instance member of List<T> (System.Collections.Generic). Call it on a list variable, such as numbers.Remove(...). 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 value you want to delete. Use the returned bool when you need to know whether anything changed.
C#
List<int> numbers = new List<int> { 1, 2, 3, 2 };
bool removed = numbers.Remove(2); // true; list is 1, 3, 2
bool missing = numbers.Remove(9); // false; list is unchanged
Call
What it does
numbers.Remove(2)
Deletes only the first 2. Other copies stay in the list.
numbers.Remove(9)
Returns false when there is no 9. No exception.
if (numbers.Remove(x)) { ... }
Run code only when something was really removed.
Remove is O(n): it scans for the item, then shifts the later items. Equality uses the default comparer, which calls Equals. For int and string that compares values (and strings are case-sensitive). For your own classes it compares object references unless you override Equals. To remove every match, use RemoveAll().
Library declarations
C#
public bool Remove(T item);
Parameters
Parameter
Description
item
Required. The value to remove. The first item equal to it is deleted. A null is allowed for reference types.
Return Value
Return Value
Description
bool
true if an item was found and removed. false if no item matched; the list is then unchanged.
C# Examples
Example 1 — Remove the First Match
The list holds two copies of 2. Remove one, then try a value that is missing.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<int> numbers = new List<int> { 1, 2, 3, 2 };
Console.WriteLine(numbers.Remove(2));
Console.WriteLine(string.Join(", ", numbers));
Console.WriteLine(numbers.Remove(9));
Console.WriteLine(numbers.Count);
}
}
Output
True
1, 3, 2
False
3
How It Works
Scan from the left.Remove(2) stops at index 1, the first 2, and deletes that one.
Gap closes.3 and the second 2 slide left, so the list is now 1, 3, 2. One 2 is still there.
Missing is false. There is no 9, so Remove(9) returns False and does not throw.
Count confirms it. Four items minus one removal leaves a Count of 3. The failed call changed nothing.
Example 2 — Remove Strings by Value
String comparison in a List is case-sensitive by default, so "ben" and "Ben" are different items.
C#
using System;
using System.Collections.Generic;
class Program {
static void Main() {
List<string> names = new List<string> { "Asha", "Ben", "Chen" };
Console.WriteLine(names.Remove("ben"));
Console.WriteLine(names.Remove("Ben"));
Console.WriteLine(string.Join(", ", names));
}
}
Output
False
True
Asha, Chen
How It Works
Exact text needed. The default string comparison respects letter case, so "ben" does not match "Ben".
No match, no change. The first call returns False and leaves all three names in place.
Exact match works."Ben" is found at index 1 and deleted, so the call returns True.
Result."Chen" moves left into index 1, and the list prints as Asha, Chen.
Example 3 — Remove an Object by Reference
For your own classes, Remove compares references. A new object with the same data is a different item.
C#
using System;
using System.Collections.Generic;
class Pet {
public string Name;
public Pet(string name) { Name = name; }
}
class Program {
static void Main() {
Pet rex = new Pet("Rex");
List<Pet> pets = new List<Pet> { rex, new Pet("Tom") };
Console.WriteLine(pets.Remove(new Pet("Rex")));
Console.WriteLine(pets.Remove(rex));
Console.WriteLine(pets.Count);
Console.WriteLine(pets[0].Name);
}
}
Output
False
True
1
Tom
How It Works
Class, not value.Pet does not override Equals, so two objects are equal only when they are the same object in memory.
Look-alike fails.new Pet("Rex") has the same name but is a brand-new object, so Remove returns False.
Same object works.rex is the very object stored in the list, so Remove(rex) finds it and returns True.
Fix for look-alikes. Override Equals and GetHashCode in Pet, or use RemoveAll(p => p.Name == "Rex") to match by data.
Frequently Asked Questions
It deletes the first item that equals the value you pass and returns true. If nothing matches it returns false and the list does not change.
No, only the first one. Use RemoveAll with a test (predicate) to delete all matches.
Remove searches by value and returns a bool. RemoveAt deletes by index, returns nothing, and throws ArgumentOutOfRangeException for a bad index.
For classes, the default equality compares references. Override Equals and GetHashCode, or remove with a predicate that compares the fields you care about.
No. Changing the list during foreach throws InvalidOperationException on the next step. Loop backwards with a for loop, or use RemoveAll.