如何获取与C#中的谓词指定的条件匹配的List的所有元素?
要获取与谓词指定的条件匹配的List的所有元素,代码如下-
示例
using System;
using System.Collections.Generic;
public class Demo {
private static bool demo(int i) {
return ((i % 3) == 0);
}
public static void Main(String[] args) {
List<int> list = new List<int>();
list.Add(9);
list.Add(15);
list.Add(20);
list.Add(40);
list.Add(50);
list.Add(60);
Console.WriteLine("List elements...");
foreach (int i in list) {
Console.WriteLine(i);
}
Console.WriteLine(" ");
List<int> res = new List<int>(list.FindAll(demo));
Console.WriteLine("List that match the conditions...");
foreach (int i in res) {
Console.WriteLine(i);
}
}
}输出结果
这将产生以下输出-
List elements... 9 15 20 40 50 60 List that match the conditions... 9 15 60
示例
让我们看另一个例子-
using System;
using System.Collections.Generic;
public class Demo {
private static bool demo(int i) {
return ((i % 100) == 5);
}
public static void Main(String[] args) {
List<int> list = new List<int>();
list.Add(900);
list.Add(1500);
list.Add(250);
list.Add(405);
Console.WriteLine("List elements...");
foreach (int i in list) {
Console.WriteLine(i);
}
Console.WriteLine(" ");
List<int> res = new List<int>(list.FindAll(demo));
Console.WriteLine("List that match the conditions...");
foreach (int i in res) {
Console.WriteLine(i);
}
}
}输出结果
这将产生以下输出-
List elements... 900 1500 250 405 List that match the conditions... 405
热门推荐
10 病人祝福语老师寄语简短
11 新娘生孩子祝福语简短
12 婆婆66岁祝福语简短
13 今天孩子高考祝福语简短
14 送彩票生日祝福语简短
15 分别祝福语简短情话英文
16 画室揭牌仪式祝福语简短
17 女友妈妈生日祝福语简短
18 离别赠言离职祝福语简短