
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# Array - Find() Method
The C# Array Find() method returns the first occurrence of an element after searching for an element that matches the condition defined by the specified predicate.
Syntax
Following is the syntax of the C# Array Find() method −
public static T? Find<T> (T[] array, Predicate<T> match);
Parameters
This method accepts the following parameters −
- array: The one dimensional zero based array to search.
- match: The predicate that defines the condition of the element to search for.
Return value
This method returns an element that matches the predicate if found; otherwise the default value for Type T.
Example 1: Find First Even Number
This is the basic example of the Find() method to display the first even number of an array −
using System; class Program { static void Main() { int[] Number = new int[] {1, 2, 3, 4, 5}; // Find the first even number int evenN = Array.Find(Number, num => num % 2 == 0); Console.WriteLine("First Even number is: " + evenN); } }
Output
Following is the output −
First Even number is: 2
Example 2: Find a String that Matches the Condition
Let's create an example that finds the first string whose character starts with 'c' using the Find() method −
using System; class Program { static void Main() { string[] names = {"Dipak", "Rhaul", "Chhavi", "Charlie"}; // Find name that starts with 'C' string namewithC = Array.Find(names, name => name.StartsWith("C")); Console.WriteLine("First name starting with 'C': " + namewithC); } }
Output
Following is the output −
First name starting with 'C': Chhavi
Example 3: What if No Match Found
Let's see another example of the Find() method and how it handles the case where no match is found in the array −
using System; class Program { static void Main() { int[] numbers = {1, 3, 5, 7 }; int firstEven = Array.Find(numbers, num => num % 2 == 0); if (firstEven == default (int)) { Console.WriteLine("No even number found."); } else { Console.WriteLine("First even number: " + firstEven); } } }
Output
Following is the output −
No even number found.
Example 4: Find a Custom Object Matching a Condition
In this example, we create a custom object that takes the person's name and age. We use the Find() function to find a person who is older than 23 −
using System; class Person { public string Name { get; set; } public int Age { get; set; } } class Program { static void Main() { Person[] people = { new Person { Name = "Dipak", Age = 25 }, new Person { Name = "Karan", Age = 30 }, new Person { Name = "Pankaj", Age = 22 } }; // Find the first person older than 23 Person personOlder23 = Array.Find(people, person => person.Age > 23); Console.WriteLine("First person older than 23: " + personOlder23.Name); } }
Output
Following is the output −
First person older than 23: Dipak