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

定義

指定した述語によって定義される条件に一致するすべての要素を削除します。Removes all the elements that match the conditions defined by the specified predicate.

public:
 int RemoveAll(Predicate<T> ^ match);
public int RemoveAll (Predicate<T> match);
member this.RemoveAll : Predicate<'T> -> int
Public Function RemoveAll (match As Predicate(Of T)) As Integer

パラメーター

match
Predicate<T> Predicate<T> Predicate<T> Predicate<T>

削除する要素の条件を定義する Predicate<T> デリゲート。The Predicate<T> delegate that defines the conditions of the elements to remove.

戻り値

List<T> から削除される要素の数。The number of elements removed from the List<T>.

例外

次の例で、RemoveAllメソッドおよび使用するその他のいくつかのメソッド、Predicate<T>汎用デリゲート。The following example demonstrates the RemoveAll method and several other methods that use the Predicate<T> generic delegate.

AList<T>文字列の作成、"saurus"で終わる (位置 1 と 5) での 2 つの 8 恐竜の名前を含むです。A List<T> of strings is created, containing 8 dinosaur names, two of which (at positions 1 and 5) end with "saurus". この例では、という名前の検索述語メソッドも定義しますEndsWithSaurus、文字列パラメーターを受け入れるし、ブール値を返しますが、入力文字列では"かどうかを示すを値します。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".

FindFindLast、およびFindAllメソッドを使用して、検索述語メソッドを使用して、一覧を検索します。The Find, FindLast, and FindAll methods are used to search the list with the search predicate method.

RemoveAll "Saurus"で終わるすべてのエントリを削除するメソッドを使用します。The RemoveAll method is used to remove all entries ending with "saurus". 各要素を順番に渡す、最初からリストを走査する、EndsWithSaurusメソッド。It traverses the list from the beginning, passing each element in turn to the EndsWithSaurus method. 場合、要素が削除、EndsWithSaurusメソッドを返します。trueします。The element is removed if the EndsWithSaurus method returns true.

注意

C# および Visual Basic の場合でない作成に必要なPredicate<string>デリゲート (Predicate(Of String) Visual Basic で) 明示的にします。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.

最後に、Existsメソッドでは、リストの"saurus"で終わる文字列がないことを確認します。Finally, the Exists method verifies that there are no strings in the list that end with "saurus".

using namespace System;
using namespace System::Collections::Generic;

// Search predicate returns true if a string ends in "saurus".
bool EndsWithSaurus(String^ s)
{
    return s->ToLower()->EndsWith("saurus");
};

void main()
{
    List<String^>^ dinosaurs = gcnew 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();
    for each(String^ dinosaur in dinosaurs )
    {
        Console::WriteLine(dinosaur);
    }

    Console::WriteLine("\nTrueForAll(EndsWithSaurus): {0}",
        dinosaurs->TrueForAll(gcnew Predicate<String^>(EndsWithSaurus)));

    Console::WriteLine("\nFind(EndsWithSaurus): {0}", 
        dinosaurs->Find(gcnew Predicate<String^>(EndsWithSaurus)));

    Console::WriteLine("\nFindLast(EndsWithSaurus): {0}",
        dinosaurs->FindLast(gcnew Predicate<String^>(EndsWithSaurus)));

    Console::WriteLine("\nFindAll(EndsWithSaurus):");
    List<String^>^ sublist = 
        dinosaurs->FindAll(gcnew Predicate<String^>(EndsWithSaurus));

    for each(String^ dinosaur in sublist)
    {
        Console::WriteLine(dinosaur);
    }

    Console::WriteLine(
        "\n{0} elements removed by RemoveAll(EndsWithSaurus).", 
        dinosaurs->RemoveAll(gcnew Predicate<String^>(EndsWithSaurus)));

    Console::WriteLine("\nList now contains:");
    for each(String^ dinosaur in dinosaurs)
    {
        Console::WriteLine(dinosaur);
    }

    Console::WriteLine("\nExists(EndsWithSaurus): {0}", 
        dinosaurs->Exists(gcnew Predicate<String^>(EndsWithSaurus)));
}

/* 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
 */
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)
    {
        return s.ToLower().EndsWith("saurus");
    }
}

/* 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
 */
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

注釈

Predicate<T>を返すメソッドにデリゲートtrueデリゲートで定義されている条件に一致するオブジェクトが渡された場合。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. 現在の要素List<T>に個別に渡される、Predicate<T>デリゲート、および条件に一致する要素から削除、List<T>します。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>.

このメソッドは、一方向の検索を実行します。そのため、このメソッドは、O (n) 操作、 nCountします。This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.

適用対象

こちらもご覧ください