C# | How to check whether a List contains the elements that match the specified conditions - GeeksforGeeks

01 Feb, 2019
List<T>.Exists(Predicate<T>) Method is used to check whether the List<T> contains elements which match the conditions defined by the specified predicate.
Properties of List:
It is different from the arrays. A list can be resized dynamically but arrays cannot.
List class can accept null as a valid value for reference types and it also allows duplicate elements.
If the Count becomes equals to Capacity then the capacity of the List increases automatically by reallocating the internal array. The existing elements will be copied to the new array before the addition of the new element.
Syntax:
public bool Exists (Predicate<T> match);
Parameter:
match: It is the Predicate<T> delegate which defines the conditions of the elements to search for.
Return Value: This method returns True if the List<T> contains one or more elements that match the conditions defined by the specified predicate otherwise it returns False.
Exception: This method will give ArgumentNullException if the match is null.
Below programs illustrate the use List<T>.Exists(Predicate<T>) Method:
Example 1:
// C# Program to check whether a List contains// the elements that match the specified conditions// defined by the predicateusing 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<T> of Integers        List<int> firstlist = new List<int>();          // 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));    }}
Output:
Elements Present in List:

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

Example 2:
// C# Program to check whether a List contains// the elements that match the specified conditions// defined by the predicateusing 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<T> of Integers        List<int> firstlist = new List<int>();          // 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));    }}
Output:
Elements Present in List:

5
7
9
11
3
17
19
 
Result: False

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.exists?view=netframework-4.7.2
CSharp-Collections-Namespace
CSharp-Generic-List
CSharp-Generic-Namespace
CSharp-method
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
C# | Delegates
Top 50 C# Interview Questions & Answers
Introduction to .NET Framework
C# | Constructors
Extension Method in C#
C# | Class and Object
C# | Abstract Classes
Common Language Runtime (CLR) in C#
C# | Encapsulation
C# | Method Overloading