I\'d like to write:
IEnumerable cars;
cars.Find(car => car.Color == \"Blue\")
Can I accomplish this with extension methods? The f
This method already exists. It's called FirstOrDefault
cars.FirstOrDefault(car => car.Color == "Blue");
If you were to implement it yourself it would look a bit like this
public static T Find(this IEnumerable enumerable, Func predicate) {
foreach ( var current in enumerable ) {
if ( predicate(current) ) {
return current;
}
}
return default(T);
}