如何检查某个项是否存在于 C# 列表集合中?
设置一个列表 −
List < string > list1 = new List < string > () {
"Lawrence",
"Adams",
"Pitt",
"Tom"
};现在使用 Contains 方法来检查某个项是否存在于列表中。
if (list1.Contains("Adams") == true) {
Console.WriteLine("Item exists!");
}以下是检查某个项是否存在于 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...
");
foreach(string list in list1) {
Console.WriteLine(list);
}
Console.Write("Finding an item in the list...
");
if (list1.Contains("Adams") == true) {
Console.WriteLine("Item exists!");
} else {
Console.WriteLine("Item does not exist!");
}
}
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP