Export (0) Print
Expand All
3 out of 10 rated this helpful - Rate this topic

List(Of T).RemoveAll Method

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

Namespace:  System.Collections.Generic
Assembly:  mscorlib (in mscorlib.dll)
'Declaration
Public Function RemoveAll ( _
	match As Predicate(Of T) _
) As Integer

Parameters

match
Type: System.Predicate(Of T)

The Predicate(Of T) delegate that defines the conditions of the elements to remove.

Return Value

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

match is Nothing.

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

NoteNote

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

Imports System
Imports System.Collections.Generic

Public Class Example

    Public Shared Sub Main()

        Dim dinosaurs As New List(Of 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()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & _
            "TrueForAll(AddressOf EndsWithSaurus: {0}", _
            dinosaurs.TrueForAll(AddressOf EndsWithSaurus))

        Console.WriteLine(vbLf & _
            "Find(AddressOf EndsWithSaurus): {0}", _
            dinosaurs.Find(AddressOf EndsWithSaurus))

        Console.WriteLine(vbLf & _
            "FindLast(AddressOf EndsWithSaurus): {0}", _
            dinosaurs.FindLast(AddressOf EndsWithSaurus))

        Console.WriteLine(vbLf & _
            "FindAll(AddressOf EndsWithSaurus):")
        Dim sublist As List(Of String) = _
            dinosaurs.FindAll(AddressOf EndsWithSaurus)

        For Each dinosaur As String In sublist
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & _
            "{0} elements removed by RemoveAll(AddressOf EndsWithSaurus).", _
            dinosaurs.RemoveAll(AddressOf EndsWithSaurus))

        Console.WriteLine(vbLf & "List now contains:")
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & _
            "Exists(AddressOf EndsWithSaurus): {0}", _
            dinosaurs.Exists(AddressOf EndsWithSaurus))

    End Sub 

    ' Search predicate returns true if a string ends in "saurus". 
    Private Shared Function EndsWithSaurus(ByVal s As String) _
        As Boolean 

        Return s.ToLower().EndsWith("saurus")
    End Function 
End Class 

' This code example produces the following output: 

'Compsognathus 
'Amargasaurus 
'Oviraptor 
'Velociraptor 
'Deinonychus 
'Dilophosaurus 
'Gallimimus 
'Triceratops 

'TrueForAll(AddressOf EndsWithSaurus: False 

'Find(AddressOf EndsWithSaurus): Amargasaurus 

'FindLast(AddressOf EndsWithSaurus): Dilophosaurus 

'FindAll(AddressOf EndsWithSaurus): 
'Amargasaurus 
'Dilophosaurus 

'2 elements removed by RemoveAll(AddressOf EndsWithSaurus). 

'List now contains: 
'Compsognathus 
'Oviraptor 
'Velociraptor 
'Deinonychus 
'Gallimimus 
'Triceratops 

'Exists(AddressOf EndsWithSaurus): False

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.