要查询 List 中的某一个值,可以使用 LINQ 查询或者遍历 List 的方法。

  1. 使用 LINQ 查询:
List<int> list = new List<int> { 1, 2, 3, 4, 5 };
int value = 3;
boolean containsValue = list.Contains(value);
  1. 遍历 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 并初始化了一些值。然后,我们定义了要查找的值 value,并使用 LINQ Contains 方法或者遍历 List 来查找该值是否存在于 List 中。最后,我们将结果保存在一个布尔变量 containsValue 中。

C# 如何在 List 中查找指定值

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

免费AI点我,无需注册和登录