This is just a curiosity question I was wondering if anyone had a good answer to:
In the .NET Framework Class Library we have for example these two methods:
public static IQueryable<TSource> Where<TSource>(
this IQueryable<TSource> source,
Expression<Func<TSource, bool>> predicate
)
public static IEnumerable<TSource> Where<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate
)
Why do they use Func<TSource, bool>
instead of Predicate<TSource>
? Seems like the Predicate<TSource>
is only used by List<T>
and Array<T>
, while Func<TSource, bool>
is used by pretty much all Queryable
and Enumerable
methods and extension methods… what's up with that?
Best Answer
While
Predicate
has been introduced at the same time thatList<T>
andArray<T>
, in .net 2.0, the differentFunc
andAction
variants come from .net 3.5.So those
Func
predicates are used mainly for consistency in the LINQ operators. As of .net 3.5, about usingFunc<T>
andAction<T>
the guideline states: