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.

Array.Exists() Process Array Condition Returns bool ["A", "B", "C"] x.StartsWith("C") true Checks each element until condition is met Returns true on first match, false if none found

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.

Updated on: 2026-03-17T07:04:36+05:30

515 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements