TheDeveloperBlog.com


C# Array.Find Method

Array.Find declaratively searches an array. We specify a Predicate type instance to determine what logic the search uses. This allows us to use a for-loop without having to maintain loop indexes. It eases maintenance.


Example. First, this program demonstrates how the Array.Find static method can be used on the Array type. This is a static method. It is found in the abstract base class for all array types.

Static Method

You call it with a qualified composite name and use the syntax "Array.Find". The first parameter is an array reference. The second parameter is a Predicate that receives an individual element and returns true or false.

True and False

And: You can use the lambda syntax to specify the Predicate definition directly in the method call.

Lambda ExpressionPredicate
C# program that uses Array.Find static method

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

This program introduces the Main entry point. 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.

Also, 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 from the syntax.

But: If nothing is found, Array.Find and Array.FindAll return the default value (null or zero).

Default

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. But you can use an anonymous method and delegate keyword.

Anonymous Functions

Internals. Internally, 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.

Thus: In the .NET Framework, the Array class is implemented with the System.Collections.Generics types.

Arrays

FindLast. The Array type offers the FindLast method along with the Find method. FindLast does the same thing but searches from the final element. It proceeds backwards, searching each preceding element in order.

This example program returns a string element in the string array. It searches for the last string that is three characters long. It does this with a Predicate that accesses the Length property.

String Length Property
C# program that uses Array.FindLast

using System;

class Program
{
    static void Main()
    {
	string[] array = { "dot", "net", "perls" };
	// Find last string of length 3.
	string result = Array.FindLast(array, s => s.Length == 3);
	Console.WriteLine(result);
    }
}

Output

net


List. The powerful and convenient List type in the System.Collections.Generic namespace provides a dynamic array. It also can use predicates with its Find method. Its Find method can be used with similar syntax.

List FindList

Summary. We used Array.Find and Array.FindAll, which return a matching value or array when they succeed. These methods are static. They can be used with arrays of any element type. They internally run for-loops to search.

For Loops

And: Using these methods results in more declarative code rather than imperative code. This can ease maintenance.

Array.FindIndex