📌  相关文章
📜  C#|如何检查列表是否包含符合指定条件的元素

📅  最后修改于: 2021-05-29 15:26:24             🧑  作者: Mango

List .Exists(Predicate )方法用于检查List 是否包含与指定谓词定义的条件匹配的元素。

列表的属性:

  • 它与数组不同。列表可以动态调整大小,但数组则不能。
  • 列表类可以接受null作为引用类型的有效值,并且还允许重复的元素。
  • 如果计数通过重新分配内部数组自动变成等于容量则的列表的容量增加。在添加新元素之前,现有元素将被复制到新数组。

句法:

public bool Exists (Predicate match);

范围:

返回值:如果List 包含一个或多个与指定谓词定义的条件匹配的元素,则此方法返回True ,否则返回False

异常:如果匹配为null,则此方法将提供ArgumentNullException。

下面的程序说明了使用List .Exists(Predicate )方法的方法:

范例1:

// C# Program to check whether a List contains
// the elements that match the specified conditions
// defined by the predicate
using System;
using System.Collections;
using System.Collections.Generic;
  
class Geeks {
  
    // function which checks whether an
    // element is even or not. Or you can
    // say it is the specified condition
    private static bool isEven(int i)
    {
        return ((i % 2) == 0);
    }
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating an List of Integers
        List firstlist = new List();
  
        // Adding elements to List
        for (int i = 1; i <= 10; i++) {
            firstlist.Add(i);
        }
  
        Console.WriteLine("Elements Present in List:\n");
  
        // Displaying the elements of List
        foreach(int k in firstlist)
        {
            Console.WriteLine(k);
        }
  
        Console.WriteLine(" ");
  
        Console.Write("Result: ");
  
        // Check the elements of firstlist that
        // match the conditions defined by predicate
        Console.WriteLine(firstlist.Exists(isEven));
    }
}

输出:

Elements Present in List:

1
2
3
4
5
6
7
8
9
10
 
Result: True

范例2:

// C# Program to check whether a List contains
// the elements that match the specified conditions
// defined by the predicate
using System;
using System.Collections;
using System.Collections.Generic;
  
class Geeks {
  
    // function which checks whether an
    // element is even or not. Or you can
    // say it is the specified condition
    private static bool isEven(int i)
    {
        return ((i % 2) == 0);
    }
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating an List of Integers
        List firstlist = new List();
  
        // Adding elements to List
        firstlist.Add(5);
        firstlist.Add(7);
        firstlist.Add(9);
        firstlist.Add(11);
        firstlist.Add(3);
        firstlist.Add(17);
        firstlist.Add(19);
  
        Console.WriteLine("Elements Present in List:\n");
  
        // Displaying the elements of List
        foreach(int k in firstlist)
        {
            Console.WriteLine(k);
        }
  
        Console.WriteLine(" ");
  
        Console.Write("Result: ");
  
        // Check the elements of firstlist that
        // match the conditions defined by predicate
        Console.WriteLine(firstlist.Exists(isEven));
    }
}

输出:

Elements Present in List:

5
7
9
11
3
17
19
 
Result: False

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.list-1.exists?view=netframework-4.7.2