List<T>.TrueForAll(Predicate<T>) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
确定 List<T> 中的每个元素是否都与指定谓词定义的条件匹配。
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> 委托。
返回
如果 List<T> 中的每个元素都与指定的谓词所定义的条件相匹配,则为 true
;否则为 false
。 如果列表没有元素,则返回值为 true
。
例外
match
为 null
。
示例
下面的示例演示 TrueForAll 了 方法以及使用 Predicate<T> 泛型委托的其他几个方法。
创建的字符串包含 8 个 List<T> 恐龙名称,其中两个 (位于位置 1 和 5) 以“saurus”结尾。 该示例还定义了一个名为 EndsWithSaurus
的搜索谓词方法,该方法接受字符串参数并返回一个布尔值,该值指示输入字符串是否以“saurus”结尾。
方法 TrueForAll 从头开始遍历列表,将每个元素依次 EndsWithSaurus
传递给 方法。 当方法返回 false
时,EndsWithSaurus
搜索将停止。
注意
在 C# 和 Visual Basic 中,无需在 Visual Basic) Predicate(Of String)
显式创建Predicate<string>
委托 (。 这些语言从上下文推断出正确的委托,并自动创建它。
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
注解
Predicate<T>是方法的true
委托,如果传递给该方法的对象与委托中定义的条件匹配,则返回 。 当前 List<T> 的元素单独传递给 Predicate<T> 委托,当委托对任何元素返回 false
时,处理将停止。 元素按顺序处理,所有调用都在单个线程上进行。
此方法是 O (n) 操作,其中 n 为 Count。