How would you refactor this method to make it more performant?
If you have refactored the code you need to prove the performance gain in numbers to get the solution :)
public static T Find<T>(
this IEnumerable<T> source,
Func<T, IEnumerable<T>> childrenSelector,
Predicate<T> condition)
{
T t = default(T);
foreach (T item in source)
{
if (condition(item))
{
return item;
}
t = childrenSelector(item).Find<T>(childrenSelector, condition);
if (!Equals(t, default(T)))
{
return t;
}
}
return t;
}
Good question states performance goals that need to be achieved as well as other restrictions. Trying to optimize something without measuring is not "performance" question or work, but most likely personal entertainment - expect question without goals/measurements to be treated as such.
\$\endgroup\$