Search for an element in array with Array.Find method in c#

 
Array.Find method searches for an element that matches the conditions defined by the specified predicate(The Predicate is a delegate to a method that returns true if the object passed to it matches the conditions defined in the delegate.), and returns the first occurrence within the entire specified Array.

Summary:

returnvalue = Array.Find(<em>Arr </em>as array,<em>func </em>as predicate);

where
Arr: The one-dimensional, zero-based Array to search.
func: The Predicate(Of T) that defines the conditions of the element to search for.

Example:

The following example search a city that contains ‘New’ in the array arrCities.

private void ArrayFind()
   {
       string[] arrCities = { "Mumbai", "New Delhi", "Londan", "Sydney", "New York" };
       string strCity = Array.Find(arrCities,SearchCity);
       MessageBox.Show("City with 'New' is = '" + strCity + "'");
   }
 
private static bool SearchCity(string _city)
   {
       if (_city.Contains("New"))
         {
            return true;
         }
       else
         {
            return false;
         }
   }