Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if an array contains the elements that match the specified conditions in C#
To check if an array contains elements that match specific conditions in C#, we can use the Array.Exists() method. This method returns true if at least one element in the array matches the specified condition, and false otherwise.
Syntax
Following is the syntax for Array.Exists() method −
public static bool Exists<T>(T[] array, Predicate<T> match);
Parameters
-
array − The one-dimensional array to search.
-
match − The predicate that defines the conditions of the elements to search for.
Return Value
Returns true if at least one element matches the conditions defined by the specified predicate; otherwise, false.
Using Array.Exists() with StartsWith() Method
The following example demonstrates how to check if array elements begin with specific letters −
using System;
public class Demo {
public static void Main() {
string[] products = { "Electronics", "Accessories", "Clothing", "Toys", "Clothing", "Furniture" };
Console.WriteLine("Products list...");
foreach(string s in products) {
Console.WriteLine(s);
}
Console.WriteLine("\nOne or more products begin with the letter 'C'? = {0}",
Array.Exists(products, ele => ele.StartsWith("C")));
Console.WriteLine("One or more products begin with 'D'? = {0}",
Array.Exists(products, ele => ele.StartsWith("D")));
Console.WriteLine("One or more products begin with the letter 'T'? = {0}",
Array.Exists(products, ele => ele.StartsWith("T")));
Console.WriteLine("One or more products begin with 'E'? = {0}",
Array.Exists(products, ele => ele.StartsWith("E")));
}
}
The output of the above code is −
Products list... Electronics Accessories Clothing Toys Clothing Furniture One or more products begin with the letter 'C'? = True One or more products begin with 'D'? = False One or more products begin with the letter 'T'? = True One or more products begin with 'E'? = True
Using Array.Exists() for Exact Match
You can also use Array.Exists() to check for exact string matches using the equality operator −
using System;
public class Demo {
public static void Main() {
string[] products = { "Electronics", "Accessories", "Clothing", "Toys", "Clothing", "Furniture" };
Console.WriteLine("Products list...");
foreach(string s in products) {
Console.WriteLine(s);
}
Console.WriteLine("\nIs the product 'Accessories' in the array? = {0}",
Array.Exists(products, ele => ele == "Accessories"));
Console.WriteLine("Is the product 'Stationery' in the array? = {0}",
Array.Exists(products, ele => ele == "Stationery"));
Console.WriteLine("Is the product 'Toys' in the array? = {0}",
Array.Exists(products, ele => ele == "Toys"));
}
}
The output of the above code is −
Products list... Electronics Accessories Clothing Toys Clothing Furniture Is the product 'Accessories' in the array? = True Is the product 'Stationery' in the array? = False Is the product 'Toys' in the array? = True
Using Array.Exists() with Numeric Arrays
The Array.Exists() method works with any array type, including numeric arrays −
using System;
public class Demo {
public static void Main() {
int[] numbers = { 10, 25, 33, 47, 52, 68, 71, 89, 95 };
Console.WriteLine("Numbers: " + string.Join(", ", numbers));
Console.WriteLine("\nAny number greater than 50? = {0}",
Array.Exists(numbers, n => n > 50));
Console.WriteLine("Any even number? = {0}",
Array.Exists(numbers, n => n % 2 == 0));
Console.WriteLine("Any number equal to 100? = {0}",
Array.Exists(numbers, n => n == 100));
}
}
The output of the above code is −
Numbers: 10, 25, 33, 47, 52, 68, 71, 89, 95 Any number greater than 50? = True Any even number? = True Any number equal to 100? = False
Conclusion
The Array.Exists() method provides an efficient way to check if an array contains elements matching specific conditions using lambda expressions. It returns true as soon as the first matching element is found, making it performance-efficient for large arrays.
