如何检查C#列表集合中是否存在某个项目?
设置列表-
List < string > list1 = new List < string > () {
"Lawrence",
"Adams",
"Pitt",
"Tom"
};现在,使用Contains方法检查项目是否在列表中退出。
if (list1.Contains("Adams") == true) {
Console.WriteLine("项目存在!");
}以下是检查C#列表中是否存在某项的代码。
示例
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
List < string > list1 = new List < string > () {
"Lawrence",
"Adams",
"Pitt",
"Tom"
};
Console.Write("List...\n");
foreach(string list in list1) {
Console.WriteLine(list);
}
Console.Write("Finding an item in the list...\n");
if (list1.Contains("Adams") == true) {
Console.WriteLine("项目存在!");
} else {
Console.WriteLine("项目不存在!");
}
}
}热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短