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.
Argument 1 The first parameter is an array reference. This is the array that we are searching.
Argument 2 The second is a Predicate that receives an element and returns true or false.
Tip The arrow separates the parameter list from the expression that returns true or false based on those arguments.
Important If nothing is found, Array.Find and Array.FindAll return the default value (null or zero).
using System;
// 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));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.
using System;
string[] array = { "dot", "net", "perls" };
// Find last string of length 3.
string result = Array.FindLast(array, s => s.Length == 3);
Console.WriteLine(result);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.
Argument 1 The first argument is the array that we are trying to find an element within.
Argument 2 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.
Also FindIndex searches from the start—it returns an index of 1. FindLastIndex goes in reverse—it returns an index of 3.
using System;
// 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]);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.
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.
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.