C# 如何在 List 中查找指定值
要查询 List 中的某一个值,可以使用 LINQ 查询或者遍历 List 的方法。
- 使用 LINQ 查询:
List<int> list = new List<int> { 1, 2, 3, 4, 5 };
int value = 3;
boolean containsValue = list.Contains(value);
- 遍历 List:
List<int> list = new List<int> { 1, 2, 3, 4, 5 };
int value = 3;
boolean containsValue = false;
foreach (int item in list)
{
if (item == value)
{
containsValue = true;
break;
}
}
在上面的例子中,我们先创建了一个 List
原文地址: https://www.cveoy.top/t/topic/mvuP 著作权归作者所有。请勿转载和采集!