Array.FindLast() Method in C#


The Array.FindLast() method in C# is used to search for an element that matches the conditions defined by the specified predicate, and returns the last occurrence within the entire Array.

Syntax

Following is the syntax −

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

Above, array is the one-dimensional, zero-based Array to search, whereas match is the Predicate<T> that defines the conditions of the element to search for.

Example

Let us now see an example to implement the Array.FindLast() 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.FindLast(arr, ele => ele.StartsWith("b",
      StringComparison.Ordinal));
      Console.Write("\nLast Occurrence...\n");
      Console.WriteLine("{0}", res);
   }
}

Output

This will produce the following output −

Array elements...
car bike truck bus

Last Occurrence...
bus
raja
Published on 07-Nov-2019 06:07:13
Advertisements