List<(Of <(T>)>).RemoveAll Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Removes all the elements that match the conditions defined by the specified predicate.

Namespace: System.Collections.Generic
Assembly: mscorlib (in mscorlib.dll)


Public Function RemoveAll ( _
    match As Predicate(Of T) _
) As Integer
public int RemoveAll(
    Predicate<T> match


Return Value

Type: System..::.Int32
The number of elements removed from the List<(Of <(T>)>).


Exception Condition

match is nullNothingnullptra null reference (Nothing in Visual Basic).


The Predicate<(Of <(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<(Of <(T>)>) are individually passed to the Predicate<(Of <(T>)>) delegate, and the elements that match the conditions are removed from the List<(Of <(T>)>).

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<(Of <(T>)>) generic delegate.

A List<(Of <(T>)>) 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.


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".

Version Information

Windows Phone OS

Supported in: 8.1, 8.0

See Also


List<(Of <(T>)>) Class

System.Collections.Generic Namespace


Predicate<(Of <(T>)>)