
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# Array - Exists() Method
The C# Array Exists() method is used to determine whether the array contains elements that match the conditions defined by the specified predicate.
Syntax
Following is the syntax of the C# Array Exists() method −
public static bool Exists<T> (T[] array, Predicate<T> match);
Parameters
This method accepts the following parameters −
- array: It represent the one dimensional, zero-based array to search.
- match: The predicate that defines the conditions of the element to search for.
Return value
This method returns a Boolean value: true if the array contains an element that matches the given predicate, false if not.
Example 1: Check if an Array Contains a Specific Number
This is the basic example of the Exists() method, that checks whether an array contains the specified number −
using System; class Program { static void Main() { int[] numbers = { 1, 2, 3, 4, 5 }; // Check if the array contains an even number bool hasEvenNumber = Array.Exists(numbers, num => num % 2 == 0); Console.WriteLine("Contains even number: " + hasEvenNumber); } }
Output
Following is the output −
Contains even number: True
Example 2: Check for an Specific String
This is another example, uses the Exists() method to check if a specified string is present in the array −
using System; class Program { static void Main() { string[] names = { "aman", "gupta", "tutorialspoint", "India" }; bool containsCharlie = Array.Exists(names, name => name == "kumar"); Console.WriteLine("Contains 'Charlie': " + containsCharlie); } }
Output
Following is the output −
Contains 'Charlie': False
Example 3: Validate an Array of Object
In this example, we create a person object with a name and age. We then use the Exists() method to validate if a person's age is greater than 24 −
using System; class Person { public string Name { get; set; } public int Age { get; set; } } class Program { static void Main() { Person[] people = { new Person { Name = "Aman", Age = 25 }, new Person { Name = "Akash", Age = 24 }, new Person { Name = "Rahul", Age = 23 } }; bool hasOlderThan24 = Array.Exists(people, person => person.Age > 24); Console.WriteLine("Contains a person older than 24: " + hasOlderThan24); } }
Output
Following is the output −
Contains a person older than 24: True
Example 4: Check for Null or Empty Strings
Following is another example of the Exists() method. Here, we check for null or empty strings in an array −
using System; class Program { static void Main() { string[] strings = { "Hello", "", "World", null }; bool hasNullOrEmpty = Array.Exists(strings, str => string.IsNullOrEmpty(str)); Console.WriteLine("Contains null or empty strings: " + hasNullOrEmpty); } }
Output
Following is the output −
Contains null or empty strings: True