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?
While Predicate
has been introduced at the same time that List<T>
and Array<T>
, in .net 2.0, the different Func
and Action
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 using Func<T>
and Action<T>
the guideline states:
Do use the new LINQ types
Func<>
andExpression<>
instead of custom delegates and predicates