List<T>.TrueForAll(Predicate<T>) Methode
Definition
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
Parameter
- match
- Predicate<T>
Der Predicate<T>-Delegat, der die Bedingungen definiert, auf die die Elemente geprüft werden sollen.The Predicate<T> delegate that defines the conditions to check against the elements.
Gibt zurück
true
, wenn jedes Element in der List<T> die vom angegebenen Prädikat definierten Bedingungen erfüllt; andernfalls false
.true
if every element in the List<T> matches the conditions defined by the specified predicate; otherwise, false
. Wenn die Liste über keine Elemente verfügt, ist der Rückgabewert true
.If the list has no elements, the return value is true
.
Ausnahmen
match
ist null
.match
is null
.
Beispiele
Im folgenden Beispiel werden die TrueForAll -Methode und verschiedene andere Methoden veranschaulicht, die einen generischen Delegaten verwenden Predicate<T> .The following example demonstrates the TrueForAll method and several other methods that use Predicate<T> generic delegate.
Eine List<T> von Zeichen folgen wird erstellt, die acht Dinosaurier Namen enthält, von denen zwei (an den Positionen 1 und 5) mit "saurus" enden.A List<T> of strings is created, containing 8 dinosaur names, two of which (at positions 1 and 5) end with "saurus". Das Beispiel definiert auch eine Such Prädikat Methode EndsWithSaurus
mit dem Namen, die einen Zeichen folgen Parameter akzeptiert und einen booleschen Wert zurückgibt, der angibt, ob die Eingabe Zeichenfolge auf "saurus" endet.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".
Die- TrueForAll Methode durchläuft die Liste von Anfang an, wobei jedes Element wiederum an die-Methode übergeben wird EndsWithSaurus
.The TrueForAll method traverses the list from the beginning, passing each element in turn to the EndsWithSaurus
method. Die Suche wird beendet, wenn die- EndsWithSaurus
Methode zurückgibt false
.The search stops when the EndsWithSaurus
method returns false
.
Hinweis
In c# und Visual Basic muss der Delegat Predicate<string>
( Predicate(Of String)
in Visual Basic) nicht explizit erstellt werden.In C# and Visual Basic, it is not necessary to create the Predicate<string>
delegate (Predicate(Of String)
in Visual Basic) explicitly. Diese Sprachen ableiten den korrekten Delegaten aus dem Kontext und erstellen ihn automatisch.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.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
Hinweise
Der Predicate<T> ist ein Delegat für eine Methode, die zurückgibt, true
Wenn das an Sie übergebenen Objekt mit den im Delegaten definierten Bedingungen übereinstimmt.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. Die Elemente der aktuellen List<T> werden einzeln an den-Delegaten übermittelt Predicate<T> , und die Verarbeitung wird beendet, wenn der Delegat false
für ein beliebiges Element zurückgibt.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. Die Elemente werden in der Reihenfolge verarbeitet, und alle Aufrufe werden in einem einzelnen Thread durchgeführt.The elements are processed in order, and all calls are made on a single thread.
Diese Methode ist ein O (n)-Vorgang, bei dem n gleich ist Count .This method is an O(n) operation, where n is Count.