Open In App

C# | Array.Find() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

This method 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:

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

Here, T is the type of element of the array.

Parameters:

array: It is the one-dimensional, zero-based array to search.

match: It is the predicate that defines the conditions of the element to search for.

Return Value: This method return the first element that matches the conditions defined by the specified predicate if it is found. Otherwise, it returns the default value for type T.

Exception: This method throws ArgumentNullException if the array is null or match is null.

Below programs illustrate the use of Array.Find(T[], Predicate) Method:

Example 1:

CSharp




// C# program to demonstrate
// Array.Find(T[], Predicate<T>)
// Method 
using System;
using System.Collections.Generic;
  
public class GFG {
  
    // Main Method
    public static void Main()
    {
  
        try {
  
            // Creating and initializing new the String
            String[] myArr = {"Sun", "Mon", "Tue", "Thu"};
  
            // Display the values of the myArr.
            Console.WriteLine("Initial Array:");
  
            // calling the PrintIndexAndValues()
            // method to print
            PrintIndexAndValues(myArr);
  
            // getting a element a with required 
            // condition using method Find()
            string value = Array.Find(myArr, 
                       element => element.StartsWith("S",
                       StringComparison.Ordinal));
  
            // Display the value of 
            // the found element.
            Console.Write("Element: ");
  
            // printing the string 
            // following the condition
            Console.Write("{0}", value);
        }
        catch (ArgumentNullException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
  
    // Defining the method
    // PrintIndexAndValues
    public static void PrintIndexAndValues(String[] myArr)
    {
        for (int i = 0; i < myArr.Length; i++) {
  
            Console.WriteLine("{0}", myArr[i]);
        }
        Console.WriteLine();
    }
}


Output:

Initial Array:
Sun
Mon
Tue
Thu

Element: Sun

Example 2:

CSharp




// C# program to demonstrate
// Array.Find(T[], Predicate<T>)
// Method 
using System;
using System.Collections.Generic;
  
public class GFG {
  
    // Main Method
    public static void Main()
    {
  
        try {
  
            // Creating and initializing new 
            // the String with the null value
            String[] myArr = null;
  
            // getting an element a with 
            // required condition
            // using method Find()
            string value = Array.Find(myArr, 
                    element => element.StartsWith("S"
                    StringComparison.Ordinal));
  
            // Display the value of 
            // the found element.
            Console.Write("Element: ");
  
            // printing the string 
            // following the condition
            Console.Write("{0}", value);
        }
  
        catch (ArgumentNullException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
  
    // Defining the method
    // PrintIndexAndValues
    public static void PrintIndexAndValues(String[] myArr)
    {
        for (int i = 0; i < myArr.Length; i++) {
  
            Console.WriteLine("{0}", myArr[i]);
        }
        Console.WriteLine();
    }
}


Output:

Exception Thrown: System.ArgumentNullException

Reference:



Last Updated : 05 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads