C# 中第一个与指定条件匹配的列表
要找出列表中与特定条件匹配的第一个出现项,代码如下:
示例
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("First Occurrence = "+list.Find(demo)); } }
输出
将生成以下输出:
List elements... 200 215 310 500 600 First Occurrence = 200
示例
现在我们来看另一个示例:
using System; using System.Collections.Generic; public class Demo { private static bool demo(int i){ return ((i % 2) == 0); } public static void Main(String[] args){ List<int> list = new List<int>(); list.Add(255); list.Add(315); list.Add(410); list.Add(500); list.Add(600); list.Add(710); list.Add(800); list.Add(1000); Console.WriteLine("List elements..."); foreach (int i in list){ Console.WriteLine(i); } Console.WriteLine("First Occurrence = "+list.Find(demo)); } }
输出
将生成以下输出:
List elements... 255 315 410 500 600 710 800 1000 First Occurrence = 410
广告