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

定義

判斷 List<T> 中的每一個項目是否符合指定之述詞所定義的條件。Determines whether every element in the List<T> matches the conditions defined by the specified predicate.

public:
 bool TrueForAll(Predicate<T> ^ match);
public bool TrueForAll (Predicate<T> match);
member this.TrueForAll : Predicate<'T> -> bool
Public Function TrueForAll (match As Predicate(Of T)) As Boolean

參數

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

Predicate<T> 委派,可定義檢查項目所根據的條件。The Predicate<T> delegate that defines the conditions to check against the elements.

傳回

如果 List<T> 中的每一個項目都符合指定之述詞所定義的條件,則為 true,否則為 falsetrue if every element in the List<T> matches the conditions defined by the specified predicate; otherwise, false. 如果清單中沒有元素,則傳回值為 trueIf the list has no elements, the return value is true.

例外狀況

範例

下列範例會示範方法TrueForAll , 以及使用Predicate<T>泛型委派的數個其他方法。The following example demonstrates the TrueForAll method and several other methods that use Predicate<T> generic delegate.

List<T>會建立字串的, 其中包含8個恐龍名稱, 其中兩個 (位於位置1和 5) 以 "saurus" 結尾。A List<T> of strings is created, containing 8 dinosaur names, two of which (at positions 1 and 5) end with "saurus". 此範例也會定義名為EndsWithSaurus的搜尋述詞方法, 它會接受字串參數, 並傳回布林值, 指出輸入字串是否以 "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".

方法會從開頭開始傳遞清單, 然後將每個專案轉EndsWithSaurus成方法。 TrueForAllThe TrueForAll method traverses the list from the beginning, passing each element in turn to the EndsWithSaurus method. EndsWithSaurusfalse方法傳回時, 會停止搜尋。The search stops when the EndsWithSaurus method returns false.

注意

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

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

備註

是方法的委派, true如果傳遞給它的物件符合委派中定義的條件, 則Predicate<T>會傳回。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>至委派, 而當委派false針對任何元素傳回時, 就會停止處理。The elements of the current List<T> are individually passed to the Predicate<T> delegate, and processing is stopped when the delegate returns false for any element. 專案會依序處理, 而且所有呼叫都是在單一執行緒上進行。The elements are processed in order, and all calls are made on a single thread.

這個方法是 O (n) 運算, 其中nCountThis method is an O(n) operation, where n is Count.

適用於

另請參閱