List.RemoveAll Method
Assembly: mscorlib (in mscorlib.dll)
The Predicate is a delegate to a method that returns true if the object passed to it matches the conditions defined in the delegate. The elements of the current List are individually passed to the Predicate delegate, and the elements that match the conditions are removed from the List.
This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.
The following code example demonstrates the RemoveAll method and several other methods that use the Predicate generic delegate.
A List of strings is created, containing 8 dinosaur names, two of which (at positions 1 and 5) end with "saurus". The code example also defines a search predicate method named EndsWithSaurus, which accepts a string parameter and returns a Boolean value indicating whether the input string ends in "saurus".
The Find, FindLast, and FindAll methods are used to search the list with the search predicate method.
The RemoveAll method is used to remove all entries ending with "saurus". It traverses the list from the beginning, passing each element in turn to the EndsWithSaurus method. The element is removed if the EndsWithSaurus method returns true.
Note: |
|---|
| In C# and Visual Basic, it is not necessary to create the Predicate<string> delegate (Predicate(Of String) in Visual Basic) explicitly. These languages infer the correct delegate from context, and create it automatically. |
Finally, the Exists method verifies that there are no strings in the list that end with "saurus".
using System; using System.Collections.Generic; public class Example { public static void Main() { List<string> dinosaurs = new List<string>(); dinosaurs.Add("Compsognathus"); dinosaurs.Add("Amargasaurus"); dinosaurs.Add("Oviraptor"); dinosaurs.Add("Velociraptor"); dinosaurs.Add("Deinonychus"); dinosaurs.Add("Dilophosaurus"); dinosaurs.Add("Gallimimus"); dinosaurs.Add("Triceratops"); Console.WriteLine(); foreach(string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } Console.WriteLine("\nTrueForAll(EndsWithSaurus): {0}", dinosaurs.TrueForAll(EndsWithSaurus)); Console.WriteLine("\nFind(EndsWithSaurus): {0}", dinosaurs.Find(EndsWithSaurus)); Console.WriteLine("\nFindLast(EndsWithSaurus): {0}", dinosaurs.FindLast(EndsWithSaurus)); Console.WriteLine("\nFindAll(EndsWithSaurus):"); List<string> sublist = dinosaurs.FindAll(EndsWithSaurus); foreach(string dinosaur in sublist) { Console.WriteLine(dinosaur); } Console.WriteLine( "\n{0} elements removed by RemoveAll(EndsWithSaurus).", dinosaurs.RemoveAll(EndsWithSaurus)); Console.WriteLine("\nList now contains:"); foreach(string dinosaur in dinosaurs) { Console.WriteLine(dinosaur); } Console.WriteLine("\nExists(EndsWithSaurus): {0}", dinosaurs.Exists(EndsWithSaurus)); } // Search predicate returns true if a string ends in "saurus". private static bool EndsWithSaurus(String s) { if ((s.Length > 5) && (s.Substring(s.Length - 6).ToLower() == "saurus")) { return true; } else { return false; } } } /* This code example produces the following output: Compsognathus Amargasaurus Oviraptor Velociraptor Deinonychus Dilophosaurus Gallimimus Triceratops TrueForAll(EndsWithSaurus): False Find(EndsWithSaurus): Amargasaurus FindLast(EndsWithSaurus): Dilophosaurus FindAll(EndsWithSaurus): Amargasaurus Dilophosaurus 2 elements removed by RemoveAll(EndsWithSaurus). List now contains: Compsognathus Oviraptor Velociraptor Deinonychus Gallimimus Triceratops Exists(EndsWithSaurus): False */
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.
Note: