C# RemoveAll Example (Delete From List)Use the RemoveAll method on the List type with a lambda expression to remove matching elements.
RemoveAll. This C# method filters and removes elements. It can be used with a lambda expression. This reduces the size of your code and improves its clarity.
Some notes. The List RemoveAll method accepts a Predicate expression for this purpose. We can read it like a sentence: remove all elements matching this condition.
First example. This example code deletes all elements with values of 2. The lambda expression, with the => syntax, matches elements with value of 2.
Lambda A lambda expression is a compact syntax form of an anonymous delegate method.
Tip You can use the delegate keyword with an optional parameter list and a method body instead of a lambda expression.
Info Usually, lambda expressions are best kept simple to preserve code clarity. For complex methods, using a full declaration is simpler.
using System; using System.Collections.Generic; class Program { static void Main() { List<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(2); list.Add(4); list.Add(5); // Remove all list items with value of 2. // The lambda expression is the Predicate. list.RemoveAll(item => item == 2); // Display results. foreach (int i in list) { Console.WriteLine(i); } } }
1 4 5
Return value. When called, RemoveAll will invoke the Predicate method passed to it. After this is done, it will return an int equal to the number of elements it removed from the List.
Count The return int is the count of items removed—so if 2 items are removed, the return value is 2.
Nothing removed If no elements were removed, RemoveAll will return 0. We can check for 0 to see if the list remained unchanged.
Join This example displays the List contents by calling the string.Join method, which supports a List argument.
using System; using System.Collections.Generic; class Program { static bool IsRemovable(string item) { return item.StartsWith("b"); } static bool IsRemovableNothingMatches(string item) { return item == "x"; } static void Main() { var items = new List<string> { "bird", "frog", "bat" }; Console.WriteLine("ITEMS: {0}", string.Join(",", items)); // Remove 2 items, the result value is 2. var result = items.RemoveAll(IsRemovable); Console.WriteLine("COUNT OF ITEM REMOVED: {0}", result); Console.WriteLine("ITEMS: {0}", string.Join(",", items)); // Nothing is removed with this call. var result2 = items.RemoveAll(IsRemovableNothingMatches); // The result is 0. Console.WriteLine("COUNT, NOTHING REMOVED: {0}", result2); } }
Notes, lambda. When reading the lambda expression, it helps to think of it as "goes to." So you can say that the RemoveAll invocation above "removes all elements where the value goes to 2."
A discussion. There are other removal methods on List like the Remove and RemoveAt methods. If you want to remove an isolated element, use those methods instead.
List Remove
Tip The simplest method—the one that is the easiest to read—is usually the best choice.
A summary. You can use the RemoveAll method on the List. We noted how to use a lambda expression as the Predicate object to this method, and how you can read lambda expressions.
Using the RemoveAll method instead of a complex loop with multiple tests and copies can make code more readable. But be careful to keep your lambda expressions as short as possible.
© 2007-2022 sam allen.
see site info on the changelog.