C# 中的谓词是什么?
我对使用谓词非常陌生,刚刚学会了如何编写:
Predicate<int> pre = delegate(int a){ a %2 == 0 };
谓词将返回什么,以及它在编程时有什么用处?
I am very new to using predicates and just learned how to write:
Predicate<int> pre = delegate(int a){ a %2 == 0 };
What will the predicate return, and how is it useful when programming?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Predicate
是一个函数式构造,提供了一种方便的方法来基本测试给定T
对象的某些内容是否为真。例如,假设我有一个类:
现在假设我有一个
List;人们
,我想知道名单中是否有名为奥斯卡的人。如果不使用
Predicate
(或 Linq,或任何其他奇特的东西),我总是可以通过执行以下操作来完成此操作:这很好,但是让我们说我想检查是否有一个名为“Ruth”的人?还是17岁的人?
使用
谓词
,我可以使用更少的代码找到这些东西:注意我说的是更少的代码,而不是更快 >。开发人员的一个常见误解是,如果某个东西只需要一行,那么它的性能一定比需要十行的东西要好。但在幕后,采用
Predicate
的Find
方法毕竟只是枚举。 Linq 的许多功能也是如此。那么让我们看一下您问题中的具体代码:
这里我们有一个
Predicate; pre
接受一个int a
并返回a % 2 == 0
。这本质上是测试偶数。这意味着:等等。这也意味着,如果您有一个
List; ints
并且您想要找到第一个偶数,您可以这样做:当然,与您可以在代码中使用的任何其他类型一样,最好为变量提供描述性名称;所以我建议将上面的
pre
更改为evenFinder
或isEven
之类的东西 - 类似的东西。那么上面的代码就清晰很多了:Predicate<T>
is a functional construct providing a convenient way of basically testing if something is true of a givenT
object.For example suppose I have a class:
Now let's say I have a
List<Person> people
and I want to know if there's anyone named Oscar in the list.Without using a
Predicate<Person>
(or Linq, or any of that fancy stuff), I could always accomplish this by doing the following:This is fine, but then let's say I want to check if there's a person named "Ruth"? Or a person whose age is 17?
Using a
Predicate<Person>
, I can find these things using a LOT less code:Notice I said a lot less code, not a lot faster. A common misconception developers have is that if something takes one line, it must perform better than something that takes ten lines. But behind the scenes, the
Find
method, which takes aPredicate<T>
, is just enumerating after all. The same is true for a lot of Linq's functionality.So let's take a look at the specific code in your question:
Here we have a
Predicate<int> pre
that takes anint a
and returnsa % 2 == 0
. This is essentially testing for an even number. What that means is:And so on. This also means, if you have a
List<int> ints
and you want to find the first even number, you can just do this:Of course, as with any other type that you can use in code, it's a good idea to give your variables descriptive names; so I would advise changing the above
pre
to something likeevenFinder
orisEven
-- something along those lines. Then the above code is a lot clearer:根据定义,谓词将始终返回布尔值。
Predicate
基本上与Func
。谓词在编程中非常有用。它们通常用于允许您在运行时提供逻辑,该逻辑可以根据需要简单或复杂。
例如,WPF 使用
Predicate
作为 ListView 的 ICollectionView 过滤的输入。这使您可以编写可以返回布尔值的逻辑,确定特定元素是否应包含在最终视图中。逻辑可以非常简单(只需在对象上返回一个布尔值)或非常复杂,一切都取决于您。The Predicate will always return a boolean, by definition.
Predicate<T>
is basically identical toFunc<T,bool>
.Predicates are very useful in programming. They are often used to allow you to provide logic at runtime, that can be as simple or as complicated as necessary.
For example, WPF uses a
Predicate<T>
as input for Filtering of a ListView's ICollectionView. This lets you write logic that can return a boolean determining whether a specific element should be included in the final view. The logic can be very simple (just return a boolean on the object) or very complex, all up to you.以下代码可以帮助您了解谓词的一些实际用途(与命名迭代器相结合)。
The following code can help you to understand some real world use of predicates (Combined with named iterators).
在 C# 中,谓词只是返回布尔值的委托。当您搜索对象集合并想要特定的内容时,它们非常有用(根据我的经验)。
我最近在使用第 3 方 Web 控件(如树视图)时遇到了它们,因此当我需要在树中查找节点时,我使用 .Find() 方法并传递一个谓词,该谓词将返回我所在的特定节点寻找。在您的示例中,如果 'a' mod 2 为 0,委托将返回 true。当然,当我在树视图中查找节点时,我会比较它的名称、文本和值属性是否匹配。当委托找到匹配项时,它会返回我正在寻找的特定节点。
In C# Predicates are simply delegates that return booleans. They're useful (in my experience) when you're searching through a collection of objects and want something specific.
I've recently run into them in using 3rd party web controls (like treeviews) so when I need to find a node within a tree, I use the .Find() method and pass a predicate that will return the specific node I'm looking for. In your example, if 'a' mod 2 is 0, the delegate will return true. Granted, when I'm looking for a node in a treeview, I compare it's name, text and value properties for a match. When the delegate finds a match, it returns the specific node I was looking for.