Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# | Check if every List element matches the predicate conditions
Next article icon

C# | Check if an array contain the elements that match the specified conditions

Last Updated : 20 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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: 

CSharp




// 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"));
    }
}
 
 
Output:
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: 

CSharp




// 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")));
}
}
 
 
Output:
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. 

Space complexity: O(n) where n is the size of the array

Reference: https://docs.microsoft.com/en-us/dotnet/api/system.array.exists?view=netcore-2.1#definition



Next Article
C# | Check if every List element matches the predicate conditions

K

Kirti_Mangal
Improve
Article Tags :
  • C#
  • CSharp-Arrays
  • CSharp-method

Similar Reads

  • C# | How to get all elements of a List that match the conditions specified by the predicate
    List<T>.FindAll(Predicate<T>) Method is used to get all the elements that 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 refe
    3 min read
  • C# | Check if every List element matches the predicate conditions
    List<T>.TrueForAll(Predicate<T>) is used to check whether every element in the List<T> matches the conditions defined by the specified predicate or not. Syntax: public bool TrueForAll (Predicate<T> match); Parameter: match: It is the Predicate<T> delegate which defines
    2 min read
  • C# | Check if the specified string is in the StringCollection
    StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace. StringCollection.Contains(String) method is used to check whether the specified string is in the St
    2 min read
  • C# | Array.BinarySearch(Array, Int32, Int32, Object, IComparer) Method
    This method searches for an element which is in a one-dimensional sorted array within a range of elements using a specified IComparer interface. Syntax: public static int BinarySearch(Array arr, int index, int length, Object value, IComparer comparer) Parameters: arr : The sorted one-dimensional Arr
    4 min read
  • C# | Array.BinarySearch(Array, Object, IComparer) Method
    This method searches for a value in a one-dimensional sorted array using a specified IComparer interface. Syntax: public static int BinarySearch(Array arr, Object val, IComparer comparer) Parameters: arr : The one-dimensional sorted array in which the search will happen. val : The object value which
    4 min read
  • C# | Array.FindAll() Method
    This method is used to retrieve all the elements that match the conditions defined by the specified predicate.Syntax: public static T[] FindAll (T[] array, Predicate match); Here, T is the type of element of the array.Parameters: array: It is the one-dimensional, zero-based array to search.match: It
    3 min read
  • C# | Array.Find() Method
    This method is used to search for an element that matches the conditions defined by the specified predicate and returns the first occurrence within the entire Array. Syntax: public static T Find (T[] array, Predicate<T> match); Here, T is the type of element of the array. Parameters: array: It
    3 min read
  • C# | Array.TrueForAll() Method
    This method is used to determine whether every element in the array matches the conditions defined by the specified predicate.Syntax: public static bool TrueForAll (T[] array, Predicate<T> match); Here, T is the type of element of the array.Parameters: array: It is the one-dimensional, zero-ba
    3 min read
  • C# Program to Check a Specified Type is an Array or Not
    In C#, an array is a group of homogeneous elements that are referred to by a common name. So in this article, we will discuss how to check the variable is of array type or not. To do this task, we use the IsArray property of the Type class. This property is used to determine whether the specified ty
    2 min read
  • C Program to Check for Majority Element in a sorted array
    Question: Write a function to find if a given integer x appears more than n/2 times in a sorted array of n integers. Basically, we need to write a function say isMajority() that takes an array (arr[] ), array’s size (n) and a number to be searched (x) as parameters and returns true if x is a majorit
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences