Array.Exists(T[], Predicate<T>) Method is used to check whether the specified array contains elements that match the conditions defined by the specified predicate.
Syntax:
public static bool Exists<T> (T[] array, Predicate<T> match);
Parameters:
array: It is a one-dimensional, zero-based Array to search.
match: It is a Predicate that defines the conditions of the elements to search for. Where T is a type of the elements present in the array.
Return Value: The return type of this method is System.Boolean. It return true if array contains one or more elements that match the conditions defined by the specified predicate. Otherwise, return false.
Exception: This method will give ArgumentNullException if the value of array is null, or if the value of match is null.
Below given are some examples to understand the implementation in a better way:
Example 1:
// C# program to illustrate the // Array.Exists(T[], Predicate<T>) Method using System; class GFG { // Main method static public void Main() { // Create and initialize array string[] language = {"Ruby", "C", "C++", "Java", "Perl", "C#", "Python", "PHP"}; // Display language array Console.WriteLine("Display the array:"); foreach(string i in language) { Console.WriteLine(i); } // Display and check the given elements // present in the array or not // Using Exists() method Console.WriteLine("Is Ruby part of language: {0}", Array.Exists(language, element => element == "Ruby")); Console.WriteLine("Is VB part of language: {0}", Array.Exists(language, element => element == "VB")); } } |
Display the array: Ruby C C++ Java Perl C# Python PHP Is Ruby part of language: True Is VB part of language: False
Example 2:
// C# program to illustrate the // Array.Exists(T[], Predicate<T>) Method using System; public class GFG { // Main method static public void Main() { // Create and initialize array string[] ds = {"Array", "Queue", "LinkedList", "Stack", "Graph" }; // Display ds array Console.WriteLine("Given Array: "); foreach(string i in ds) { Console.WriteLine(i); } // Display and check the given elements with the // specified letter is present in the array or not // Using Exists() method Console.WriteLine("Is element start with L letter is present in array: {0}", Array.Exists(ds, element => element.StartsWith("L"))); Console.WriteLine("Is element start with O letter is present in array: {0}", Array.Exists(ds, element => element.StartsWith("O"))); } } |
Given Array: Array Queue LinkedList Stack Graph Is element start with L letter is present in array: True Is element start with O letter is present in array: False
Note: This method is an O(n) operation, where n is the Length of array.
Reference: https://docs.microsoft.com/en-us/dotnet/api/system.array.exists?view=netcore-2.1#definition
Recommended Posts:
- C# | How to check whether a List contains the elements that match the specified conditions
- C# | How to get all elements of a List that match the conditions specified by the predicate
- C# | Remove all elements of a List that match the conditions defined by the predicate
- C# | How to get the last occurrence of the element in the List that match the specified conditions
- C# | Remove elements from a SortedSet that match the predicate
- C# | Check if every List element matches the predicate conditions
- C# | Remove elements from a HashSet with conditions defined by the predicate
- C# | Check if HashSet and the specified collection contain the same elements
- C# | Check if SortedSet and the specified collection contain the same elements
- C# | Check if SortedSet and a specified collection share common elements
- C# | Check if a HashSet and a specified collection share common elements
- C# | First occurrence in the List that matches the specified conditions
- C# | Check if an array is read-only or not
- C# | Check if an Array has fixed size or not
- C# | Copying BitArray elements to an Array
- C# | Copying the Collection<T> elements to an array
- C# | Copying the elements of ArrayList to a new array
- Copying the Queue elements to 1-D Array in C#
- C# | Check if an array is synchronized (thread safe) or not
- Total number of elements in a specified dimension of an Array in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

