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.
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".
string[] names = { "Steve", "Bill", "Bill Gates", "Ravi", "Mohan", "Salman", "Boski" };
var stringToFind = "Bill";
var result = Array.Find(names, element => element == stringToFind); // returns "Bill"
The following example returns the first element that starts with "B".
string[] names = { "Steve", "Bill", "Bill Gates", "James", "Mohan", "Salman", "Boski" };
var result = Array.Find(names, element => element.StartsWith("B")); // returns Bill
The following example returns the first element, whose length is five or more.
string[] names = { "Steve", "Bill", "James", "Mohan", "Salman", "Boski" };
var result = Array.Find(names, element => element.Length >= 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.
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".
string[] names = { "Steve", "Bill", "bill", "James", "Mohan", "Salman", "Boski" };
var stringToFind = "bill";
string[] result = Array.FindAll(names, element => element.ToLower() == stringToFind); // return Bill, bill
The following example finds all elements that start with B.
string[] names = { "Steve", "Bill", "James", "Mohan", "Salman", "Boski" };
string[] result = Array.FindAll(names, element => element.StartsWith("B")); // return Bill, Boski
The following example finds all elements whose length is five or more.
string[] names = { "Steve", "Bill", "James", "Mohan", "Salman", "Boski" };
string[] result = Array.FindAll(names, element => element.Length >= 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.
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".
string[] names = { "Steve", "Bill", "Bill Gates", "Ravi", "Mohan", "Salman", "Boski" };
var stringToFind = "Bill";
var result = Array.FindLast(names, element => element.Contains(stringToFind)); // returns "Boski"
The following example returns the last element that starts with "B".
string[] names = { "Steve", "Bill", "James", "Mohan", "Salman", "Boski" };
var result = Array.FindLast(names, element => element.StartsWith("B")); // returns Boski
The following example returns the first element, whose length is five or more.
string[] names = { "Steve", "Bill", "Bill Gates", "James", "Mohan", "Salman", "Boski" };
result = Array.FindLast(names, element => element.Length >= 5); // returns Boski
Thus, choose the appropriate method as per your requirement to search for an element in an array in C#.
- How to count elements in C# array?
- How to combine two arrays without duplicate values in C#?
- How to get a comma separated string from an array in C#?
- How to remove duplicate values from an array in C#?
- How to sort an array in C#?
- How to sort object array by specific property in C#?
- Difference between Array and ArrayList