c#中的谓词是什么? [英] What is a predicate in c#?
问题描述
我对使用谓词很陌生,刚刚学会了如何编写:
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?
推荐答案
Predicate
是一个函数式结构,提供了一种基本测试给定 T 是否为真的便捷方法
对象.
Predicate<T>
is a functional construct providing a convenient way of basically testing if something is true of a given T
object.
例如假设我有一堂课:
class Person {
public string Name { get; set; }
public int Age { get; set; }
}
现在假设我有一个 List
,我想知道列表中是否有人叫Oscar.
Now let's say I have a List<Person> people
and I want to know if there's anyone named Oscar in the list.
不使用Predicate
(或Linq,或任何其他花哨的东西),我总是可以通过执行以下操作来完成此操作:
Without using a Predicate<Person>
(or Linq, or any of that fancy stuff), I could always accomplish this by doing the following:
Person oscar = null;
foreach (Person person in people) {
if (person.Name == "Oscar") {
oscar = person;
break;
}
}
if (oscar != null) {
// Oscar exists!
}
这很好,但是假设我想检查是否有一个叫Ruth"的人?还是一个 17 岁的人?
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?
使用 Predicate
,我可以使用更少的代码找到这些东西:
Using a Predicate<Person>
, I can find these things using a LOT less code:
Predicate<Person> oscarFinder = (Person p) => { return p.Name == "Oscar"; };
Predicate<Person> ruthFinder = (Person p) => { return p.Name == "Ruth"; };
Predicate<Person> seventeenYearOldFinder = (Person p) => { return p.Age == 17; };
Person oscar = people.Find(oscarFinder);
Person ruth = people.Find(ruthFinder);
Person seventeenYearOld = people.Find(seventeenYearOldFinder);
注意我说的是更少的代码,而不是更快.开发人员的一个常见误解是,如果某样东西只需要一行,它的性能肯定比需要 10 行的东西更好.但在幕后,采用 Predicate
的 Find
方法毕竟只是枚举.Linq 的许多功能也是如此.
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 a Predicate<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:
Predicate<int> pre = delegate(int a){ return a % 2 == 0; };
这里我们有一个 Predicate
接受一个 int a
并返回 a % 2 == 0
.这本质上是测试偶数.这意味着:
Here we have a Predicate<int> pre
that takes an int a
and returns a % 2 == 0
. This is essentially testing for an even number. What that means is:
pre(1) == false;
pre(2) == true;
等等.这也意味着,如果你有一个 List
并且你想找到第一个偶数,你可以这样做:
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:
int firstEven = ints.Find(pre);
当然,与您可以在代码中使用的任何其他类型一样,为您的变量提供描述性名称是个好主意;所以我建议将上面的 pre
更改为 evenFinder
或 isEven
之类的东西 - 沿着这些路线的东西.那么上面的代码就清晰了很多:
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 like evenFinder
or isEven
-- something along those lines. Then the above code is a lot clearer:
int firstEven = ints.Find(evenFinder);
这篇关于c#中的谓词是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!