本文整理匯總了C#中System.Collections.Generic.List<T>.TrueForAll方法的典型用法代碼示例。如果您正苦於以下問題:C# List<T>.TrueForAll方法的具體用法?C# List<T>.TrueForAll怎麽用?C# List<T>.TrueForAll使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Collections.Generic.List<T>
的用法示例。
在下文中一共展示了List<T>.TrueForAll方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
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");
}
}
輸出:
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