C# Array.Find Examples, Search Array With Lambda

Use the Array.Find and Array.FindLast methods. Search arrays with lambda Predicates.
Array.Find. This method searches an array (with declarative syntax). We specify a Predicate type instance to determine what logic the search uses.Array
Finding notes. Array.Find allows us to use a for-loop without having to maintain loop indexes. It eases maintenance. We can also use FindLast and FindIndex.
Find example. This program shows 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.

Parameters: The first parameter is an array reference. The second is a Predicate that receives an element and returns true or false.


Main: We use Array.Find and FindAll to perform searches on the array. The second argument to Find and FindAll is a Predicate instance.

Lambda: The => token separates the parameter list from the expression that returns true or false based on those arguments.

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

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
FindLast example. FindLast does the same thing as Find but searches from the final element. It proceeds backwards, searching each preceding element in order.

Here: This example program returns a string element in the string array. It searches for the last string that is 3 characters long.

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
FindIndex example. Imperative searching of arrays is fast. But sometimes, a declarative method call to locate an index is useful. FindIndex and FindLastIndex are helpful.

Arguments: The first argument is the array. The second argument is a lambda expression, which fits the requirement of a predicate.

Note: If the right-side expression evaluates to true, the element matches. And its index is returned.

Info: The lambda expressions are used to locate elements with a value of 6. This makes sense for a short example program.

Finally: FindIndex searches from the start—it returns an index of 1. FindLastIndex goes in reverse—it returns an index of 3.

C# program that uses Array.FindIndex method using System; class Program { static void Main() { // Use this input array. int[] array = { 5, 6, 7, 6 }; // Use FindIndex method with predicate. int index1 = Array.FindIndex(array, item => item == 6); // Use LastFindIndex method with predicate. int index2 = Array.FindLastIndex(array, item => item == 6); // Write results. Console.WriteLine("{0} = {1}", index1, array[index1]); Console.WriteLine("{0} = {1}", index2, array[index2]); } } Output 1 = 6 3 = 6
Internals. Find does a forward linear search with a for-loop and tests each element with the Predicate. FindAll 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.

List, Find. The powerful List type in the System.Collections.Generic namespace provides a dynamic array. It also can use predicates with its Find method.List Find
Notes, predicate. The Predicate type is a generic type that encapsulates a method that must return a Boolean. We usually use the lambda syntax—but we can use an anonymous function.Anonymous Functions
A 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.
Final notes. Higher-order procedures (like Array.FindIndex) can be help code reuse. It is possible to reuse more logic by only changing the predicate or lambda expression.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to