You can use the Find method of an array type. From .NET 3.5 and higher.
public static T Find<T>( T[] array, Predicate<T> match )
Here are some examples:
// we search an array of strings for a name containing the letter "a": static void Main() { string[] names = { "Rodney", "Jack", "Jill" }; string match = Array.Find (names, ContainsA); Console.WriteLine (match); // Jack } static bool ContainsA (string name) { return name.Contains ("a"); }
Here is the same code, abbreviated anonymous method:
string[] names = { "Rodney", "Jack", "Jill" }; string match = Array.Find (names, delegate (string name) { return name.Contains ("a"); } );
The lambda expression shortens it further:
string[] names = { "Rodney", "Jack", "Jill" }; string match = Array.Find (names, n => n.Contains ("a"));
Yuliia Ashomok Nov 07 '14 at 2:09 a.m. 2014-11-07 14:09
source share