Tutorialsteacher

Follow Us

Search value in an array in C#

Often you need to search element(s) in an array based on some logic in C#. Use the Array.Find() or Array.FindAll() or Array.FindLast() methods to search for an elements that match with the specified condition.

Array.Find()

The Array.Find() method searches for an element that matches the specified conditions using predicate delegate, and returns the first occurrence within the entire Array.

Syntax:
public static T Find<T>(T[] array, Predicate<T> match);

As per the syntax, the first parameter is a one-dimensional array to search and the second parameter is the predicate deligate which can be a lambda expression. It returns the first element that satisfy the conditions defined by the predicate expression; otherwise, returns the default value for type T.

The following example finds the first element that matches with string "Bill".

Example: Find literal value
string[] names = { "Steve", "Bill", "Bill Gates", "Ravi", "Mohan", "Salman", "Boski" };
var stringToFind = "Bill";
 
var result = Array.Find(names, element =&gt; element == stringToFind); // returns "Bill"

The following example returns the first element that starts with "B".

Example: Find elements that starts with B
string[] names = { "Steve", "Bill", "Bill Gates", "James", "Mohan", "Salman", "Boski" };
 
var result = Array.Find(names, element =&gt; element.StartsWith("B")); // returns Bill

The following example returns the first element, whose length is five or more.

Example: Find by length
string[] names = { "Steve", "Bill", "James", "Mohan", "Salman", "Boski" };

var result = Array.Find(names, element => element.Length &gt;= 5); // returns Steve

Notice that the Array.Find() method only returns the first occurrence and not all matching elements. Use the Array.FindAll() method to retrieve all matching elements.

Array.FindAll()

The Array.FindAll() method returns all elements that match the specified condition.

Syntax:
public static T[] FindAll<T>(T[] array, Predicate<T> match)

As per the syntax, the first parameter is a one-dimensional array to search and the second parameter is the predicate deligate which can be a lambda expression. It returns all the elements that satisfy the condition defined by the predicate expression.

The following example finds all elements that match with "Bill" or "bill".

Example: Find literal values
string[] names = { "Steve", "Bill", "bill", "James", "Mohan", "Salman", "Boski" };
var stringToFind = "bill";
           
string[] result = Array.FindAll(names, element =&gt; element.ToLower() == stringToFind); // return Bill, bill

The following example finds all elements that start with B.

Example: Find all elements starting with B
string[] names = { "Steve", "Bill", "James", "Mohan", "Salman", "Boski" };

string[]  result = Array.FindAll(names, element =&gt; element.StartsWith("B")); // return Bill, Boski

The following example finds all elements whose length is five or more.

Example: Find elements by length
string[] names = { "Steve", "Bill", "James", "Mohan", "Salman", "Boski" };
 
string[] result = Array.FindAll(names, element => element.Length &gt;= 5); // returns Steve, James, Mohan, Salman, Boski

Array.FindLast()

The Array.Find() method returns the first element that matches the condition. The Array.FindLast() method returns the last element that matches the specified condition in an array.

Syntax:
public static T FindLast<T>(T[] array, Predicate<T> match)

As per the syntax, the first parameter is a one-dimensional array to search and the second parameter is the predicate deligate which can be a lambda expression. It returns the last element that satisfy the condition defined by the predicate expression. If not found then returns the default value for type T.

The following example finds the last element that matches with "Bill".

Example: Find last element
string[] names = { "Steve", "Bill", "Bill Gates", "Ravi", "Mohan", "Salman", "Boski" };
var stringToFind = "Bill";
 
var result = Array.FindLast(names, element =&gt; element.Contains(stringToFind)); // returns "Boski"

The following example returns the last element that starts with "B".

Example: Find last element starting with B
string[] names = { "Steve", "Bill", "James", "Mohan", "Salman", "Boski" };
 
var result = Array.FindLast(names, element =&gt; element.StartsWith("B")); // returns Boski

The following example returns the first element, whose length is five or more.

Example: Find last element by length
string[] names = { "Steve", "Bill", "Bill Gates", "James", "Mohan", "Salman", "Boski" };

result = Array.FindLast(names, element => element.Length &gt;= 5); // returns Boski

Thus, choose the appropriate method as per your requirement to search for an element in an array in C#.