List<'T>.RemoveAll Method (Predicate<'T>)
Removes all the elements that match the conditions defined by the specified predicate.
Assembly: mscorlib (in mscorlib.dll)
The Predicate<'T> 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<'T> are individually passed to the Predicate<'T> delegate, and the elements that match the conditions are removed from the List<'T>.
This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.
The following example demonstrates the Predicate<'T> generic delegate.method and several other methods that use the
A List<'T> of strings is created, containing 8 dinosaur names, two of which (at positions 1 and 5) end with "saurus". The 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 EndsWithSaurus method. The element is removed if the EndsWithSaurus method returns true.method is used to remove all entries ending with "saurus". It traverses the list from the beginning, passing each element in turn to the
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".
Available since 8
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 5.0
Windows Phone Silverlight
Available since 8.0
Available since 8.1