检查每个List元素是否匹配C#中的谓词条件
要检查每个List元素是否匹配谓词条件,代码如下-
示例
using System; using System.Collections.Generic; public class Demo { private static bool demo(int i) { return ((i % 10) == 0); } public static void Main(String[] args) { List<int> list = new List<int>(); list.Add(200); list.Add(215); list.Add(310); list.Add(500); list.Add(600); Console.WriteLine("List elements..."); foreach (int i in list) { Console.WriteLine(i); } Console.WriteLine(list.TrueForAll(demo)); } }
输出结果
这将产生以下输出-
List elements... 200 215 310 500 600 False
示例
让我们看另一个例子-
using System; using System.Collections.Generic; public class Demo { private static bool demo(int i) { return ((i % 10) == 0); } public static void Main(String[] args) { List<int> list = new List<int>(); list.Add(100); list.Add(200); list.Add(300); list.Add(400); list.Add(500); list.Add(600); list.Add(700); list.Add(800); list.Add(900); list.Add(1000); Console.WriteLine("List elements..."); foreach (int i in list) { Console.WriteLine(i); } Console.WriteLine(list.TrueForAll(demo)); } }
输出结果
这将产生以下输出-
List elements... 100 200 300 400 500 600 700 800 900 1000 True