C#查询List中的某一个值
要查询List中的某一个值,可以使用LINQ查询或者遍历List的方法。
- 使用LINQ查询:
List<int> list = new List<int> { 1, 2, 3, 4, 5 };
int value = 3;
bool containsValue = list.Contains(value);
- 遍历List:
List<int> list = new List<int> { 1, 2, 3, 4, 5 };
int value = 3;
bool containsValue = false;
foreach (int item in list)
{
if (item == value)
{
containsValue = true;
break;
}
}
在上面的例子中,我们先创建了一个List

原文地址: http://www.cveoy.top/t/topic/69D 著作权归作者所有。请勿转载和采集!