Array.FindAll() Method in C#


The Array.FindAll() method in C# is used to retrieve all the elements that match the conditions defined by the specified predicate.

Syntax

Following is the syntax −

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

Example

Let us now see an example to implement the Array.FindAll() 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.FindAll(arr, ele => ele.StartsWith("b",
      StringComparison.Ordinal));
      Console.Write("
All Searched elements...
");       for (int i = 0; i < res.Length; i++) {          Console.WriteLine("{0}", res[i]);       }       Console.WriteLine();    } }

Output

This will produce the following output −

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

Updated on: 08-Nov-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements