为什么Func键&LT; T,布尔&GT; ;? T&gt;将代替predicate&LT [英] Why Func<T,bool> instead of Predicate<T>?
问题描述
这仅仅是一个好奇的问题,我想知道如果任何人有一个很好的回答:
This is just a curiosity question I was wondering if anyone had a good answer to:
在.NET Framework类库,我们有例如这两种方法:
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
)
为什么他们使用 Func键&LT; TSource,布尔&GT;
而不是 predicate&LT; TSource&GT;
?好像 predicate&LT; TSource&GT;
仅由名单,其中,T&GT;
和阵列&LT; T&GT;
,而 Func键&LT; TSource,布尔&GT;
用于由pretty的多少均可查询
和可枚举
方法和扩展方法...有什么回事?
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?
推荐答案
在 predicate
已经出台,同时该名单,其中; T&GT;
和阵列&LT; T&GT;
,在.NET 2.0中,不同的 Func键
和动作
变种来自.NET 3.5。
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.
因此,那些 Func键
predicates主要用于在LINQ运营商的一致性。作为.NET 3.5,关于使用 Func键&LT; T&GT;
和动作&LT; T&GT;
的<一个href="http://blogs.msdn.com/mirceat/archive/2008/03/13/linq-framework-design-guidelines.aspx">guideline国家的:
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:
确实使用了新的LINQ类型 Func键&LT;&GT;
和
防爆pression&LT;&GT;
而不是定制
代表和predicates
Do use the new LINQ types
Func<>
andExpression<>
instead of custom delegates and predicates
这篇关于为什么Func键&LT; T,布尔&GT; ;? T&gt;将代替predicate&LT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!