System.Predicate:这真的是它的意思吗,还是它的用途?

发布于 2024-11-25 08:03:34 字数 369 浏览 1 评论 0原文

MSDN 定义 System.Predicate 这样

表示定义一组条件并确定指定对象是否满足这些条件的方法。

这真的是它的意思吗,还是它通常的用途?因为对我来说,它看起来就像一个预定义的委托,其方法必须采用 type Tobject 并返回bool - 仅此而已。

我错过了什么吗?

MSDN defines System.Predicate this way:

Represents the method that defines a set of criteria and determines whether the specified object meets those criteria.

Is this really what it means, or just what it is typically used for? Because to me it just looks like a predefined delegate whose method must take an object of type T and return a bool - and nothing more.

Am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

国产ˉ祖宗 2024-12-02 08:03:34

CLR 不强制类型名称的语义。

所以,是的,Predicate 只是一个接受 T 并返回 bool 的委托,但它应该用在需要谓词(对特定条件的测试)是预期的。程序员有责任遵守该约定。例如,如果您需要一个没有预定义语义的类似委托,则可以使用 Func

对于编译器来说,PredicateFunc 之间没有功能差异。但对于阅读您的代码的其他开发人员来说,它提供了一个重要的提示,告诉您代码应该做什么(只要您正确使用它)。

同样,没有什么可以阻止我使用 System.DayOfWeek 存储 1 到 7 之间的任意值,该值实际上并不代表一周中的某一天。这将是一件愚蠢的事情,但编译器肯定会让我这样做。由您来确保您的代码有意义,编译器无法为您做到这一点。

The CLR doesn't enforce semantics of type names.

So yes, Predicate<T> is just a delegate taking a T and returning a bool, but it's meant to be used in places where a predicate (a test for a certain condition) is expected. It's up to the programmer to respect that convention. If you need a similar delegate without a predefined semantic meaning, you could use Func<T, bool>, for example.

To the compiler, there is no functional difference between a Predicate<T> or a Func<T, bool>. But to another developer reading your code, it provides an important hint as to what your code is supposed to do, provided you used it correctly.

Similarly, there's nothing to stop me from using System.DayOfWeek to store an arbitrary value between 1 and 7 that doesn't actually represent a day of the week. It would be a stupid thing to do, but the compiler will certainly let me. It's up to you to make sure your code makes sense, the compiler can't do that for you.

后知后觉 2024-12-02 08:03:34

这就是我的谓词,该术语借用自谓词逻辑

那么当然可以使其他函数(严格来说不是谓词)具有相同的函数签名。

That is what i predicate is, the term is borrowed from predicate logics.

Then it is of course possible to make other functions, which are not strictly predicates, that have the same function signature.

甚是思念 2024-12-02 08:03:34

您认为它是预定义委托是正确的,但它最常见的用途是可扩展性,以确定方法调用外部是否满足条件。

现在,许多人更喜欢使用 Func 而不是 Predicate,因为它在框架本身中更常见。

You're spot on in thinking it's a predefined delegate but it's most common usage is in extensibility to determine outside a method call whether a condition is met.

Many now prefer to use the Func<T,bool> rather than Predicate<T> given it's more common usage within the framework itself.

别念他 2024-12-02 08:03:34

我认为该定义在特定用途中强制使用特殊原型,而委托可以用作任何函数,只有 System.Predicate 可以用于集合谓词函数(我想说它就像 C++ 指针函数原型)

I think the definition forces a special prototype in specific usage, while the delegate can be used as any function, only a System.Predicate can be used for a collection predicate function (i would say it is like a C++ pointer function prototype)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文