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
Func<TSource, bool> is used by pretty much all
Enumerable methods and extension methods... what's up with that?
Predicate has been introduced at the same time that
Array<T>, in .net 2.0, the different
Action variants come from .net 3.5.
Func predicates are used mainly for consistency in the LINQ operators. As of .net 3.5, about using
Action<T> the guideline states:
Do use the new LINQ types
Expression<>instead of custom delegates and predicates
The advice (in 3.5 and above) is to use the
Func<...> - for the "why?" - one advantage is that "
Predicate<T>" is only meaningful if you know what "predicate" means - otherwise you need to look at object-browser (etc) to find the signatute.
Func<T,bool> follows a standard pattern; I can immediately tell that this is a function that takes a
T and returns a
bool - don't need to understand any terminology - just apply my truth test.
For "predicate" this might have been OK, but I appreciate the attempt to standardise. It also allows a lot of parity with the related methods in that area.