如何检查 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