为什么Func键< T,布尔> ;? T>将代替predicate&LT布尔、LT、Func、predicate

由网友(跟着风走把孤独当自由)分享简介:这仅仅是一个好奇的问题,我想知道如果任何人有一个很好的回答:This is just a curiosity question I was wondering if anyone had a good answer to:在.NET Framework类库,我们有例如这两种方法:In the .NET Frame...

这仅仅是一个好奇的问题,我想知道如果任何人有一个很好的回答:

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; 的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<> and Expression<> instead of custom delegates and predicates

阅读全文

相关推荐

最新文章