Array.Find() Method in C#


The Array.Find() method in C# 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

Following is the syntax −

public static T Find<T> (T[] array, Predicate<T> match);

Above, the array is the one-dimensional, zero-based array to search, whereas match is the predicate that defines the conditions of the element to search for.

Example

Let us now see an example to implement the Array.Find() method −

using System;
public class Demo{
   public static void Main(){
      Console.WriteLine("Array elements...");
      string[] arr = { "car", "bike", "truck", "bus"};
      for (int i = 0; i < arr.Length; i++){
         Console.Write("{0} ", arr[i]);
      }
      Console.WriteLine();
      string res = Array.Find(arr, ele => ele.StartsWith("t",
      StringComparison.Ordinal));
      Console.Write("Searched element...");
      Console.Write("{0}", res);
   }
}

Output

This will produce the following output −

Array elements...
car bike truck bus
Searched element...truck

Updated on: 08-Nov-2019

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements