如何检查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("项目不存在!");
}
}
}热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短