C# Array.Find Method

Find icon

You want to use the Array.Find method in the .NET Framework and C# language to declaratively search the arrays in your program. You can specify a Predicate type instance to determine what logic the search uses, and this allows you to use a for-loop without having to maintain loop indexes, easing maintenance.

Example

Note

First, this program demonstrates how the Array.Find static method can be used on the Array type. This is a static method, which is found in the abstract base class for all array types. You call it with a qualified composite name and use the syntax "Array.Find". The first parameter is an array reference of any type elements, and the second parameter is a Predicate that receives an individual element and returns true or false. You can use the lambda syntax to specify the Predicate definition.

Static Method True Value False Value

This C# example program uses the Array.Find method. It searches an array.

Program that uses Array.Find static method [C#]

using System;

class Program
{
    static void Main()
    {
	//
	// Use this array of string references.
	//
	string[] array1 = { "cat", "dog", "carrot", "bird" };
	//
	// Find first element starting with substring.
	//
	string value1 = Array.Find(array1,
	    element => element.StartsWith("car", StringComparison.Ordinal));
	//
	// Find first element of three characters length.
	//
	string value2 = Array.Find(array1,
	    element => element.Length == 3);
	//
	// Find all elements not greater than four letters long.
	//
	string[] array2 = Array.FindAll(array1,
	    element => element.Length <= 4);

	Console.WriteLine(value1);
	Console.WriteLine(value2);
	Console.WriteLine(string.Join(",", array2));
    }
}

Output

carrot
cat
cat,dog,bird
Main method

Description. This program introduces the Main entry point and in this method we use the Array.Find method and the Array.FindAll method to perform searches on the array. The second argument to Find and FindAll is a Predicate instance, but you can specify this inline with lambda syntax.

The => token in the syntax simply separates the parameter list from the expression that returns true or false based on those arguments. The Boolean return type is inferred. If nothing is found, Array.Find and Array.FindAll return the default value (null or zero).

Predicate type details. The Predicate type is a generic type that encapsulates a method that must return a Boolean result. The easiest way to specify the Predicate is with the lambda syntax as shown; however, you can use an anonymous method and delegate keyword if you want.

Predicate Type Lambda Expression Anonymous Function Example

Implementation

.NET Framework information

Let's delve into the .NET Framework to look at the implementation of Find and FindAll on the Array type. The Find method does a forward linear search with a for-loop and tests each element with the Predicate. The FindAll method uses the List type and simply adds each match to this internal list variable.

Note: This means that in the .NET Framework, the Array class is implemented with the System.Collections.Generics types.

List Find method

The extremely powerful and convenient List type in the System.Collections.Generic namespace provides a dynamic array and it also can use predicates with its Find method. In other words, its Find method can be used with only a few alterations from the example shown here.

List Find Method List Examples

Summary

The C# programming language

We looked at the Array.Find method and the similar Array.FindAll method, which return a matching value or array when they succeed. These methods are static methods and can be used with arrays of any element type, and internally run for-loops to search. Using these methods results in more declarative code rather than imperative code, which doesn't improve performance normally but can ease maintenance.

Array Types
.NET