不是 Func和谓词编译后同样的事情吗?
尚未启动反射器来查看差异,但在比较 Func
与 Predicate
时,人们希望看到完全相同的编译代码
我想没有什么区别,因为两者都采用通用参数并返回 bool?
Haven't fired up reflector to look at the difference but would one expect to see the exact same compiled code when comparing Func<T, bool>
vs. Predicate<T>
I would imagine there is no difference as both take a generic parameter and return bool?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更灵活的
Func
系列仅在 .NET 3.5 中出现,因此它将在功能上复制之前出于必要而必须包含的类型。(加上名称
Predicate
向源代码的读者传达预期用途)The more flexible
Func
family only arrived in .NET 3.5, so it will functionally duplicate types that had to be included earlier out of necessity.(Plus the name
Predicate
communicates the intended usage to readers of the source code)即使没有泛型,您也可以拥有签名和返回类型相同的不同委托类型。 例如:
在上面的示例中,两个非泛型类型具有相同的签名和返回类型。 (实际上也与
Predicate
和Func
相同)。 但正如我试图指出的,两者的“含义”是不同的。这有点像我创建两个类,
class Car { string Color; 小数价格; }
和class Person { string FullName; 十进制身体质量指数; }
,那么仅仅因为它们都包含一个字符串
和一个小数
,并不意味着它们是“相同”类型。Even without generics, you can have different delegate types that are identical in signatures and return types. For example:
In the above example, the two non-generic types have the same signature and return type. (And actually also the same as
Predicate<string>
andFunc<string, bool>
). But as I tried to indicate, the "meaning" of the two are different.This is somewhat like if I make two classes,
class Car { string Color; decimal Price; }
andclass Person { string FullName; decimal BodyMassIndex; }
, then just because both of them hold astring
and adecimal
, that doesn't mean they're the "same" type.它们具有相同的签名,但仍然是不同的类型。
They share the same signature, but they're still different types.
罗伯特·S. (Robert S.) 是完全正确的; 例如:-
Robert S. is completely correct; for example:-