我很新的使用predicates和刚学会写:
I am very new to using predicates and just learned how to write:
Predicate<int> pre = delegate(int a){ a %2 == 0 };
什么会在predicate回报,以及它是如何用编程的时候?
What will the predicate return, and how is it useful when programming?
predicate&LT; T&GT;
是一种功能性结构提供的,如果事情基本上测试一种简便的方法是一个给定 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; }
}
现在,让我们说我有一个名单,其中,人物&GT;人们
,我想知道是否有任何人名叫奥斯卡在列表中。
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&LT;人&GT;
(或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!
}
这是好的,但让我们说我要检查是否有一个名为露丝的人吗?还是一个人的年龄是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&LT;人&GT;
,我可以用少了很多code找到这些东西:
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);
请注意,我说了很多的少code 的,没有很多的更快的。一个常见的误解开发人员的是,如果事情占一行,就必须执行比一些需要十行更好。但在幕后,在查找
的方法,这需要一个 predicate&LT; T&GT;
,只是后枚举所有。的也是如此大量的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.
现在,让我们来看看在你的问题的具体code:
So let's take a look at the specific code in your question:
Predicate<int> pre = delegate(int a){ return a % 2 == 0; };
在这里,我们有一个 predicate&LT; INT&GT; pre
接受一个 INT一个
并返回 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;
等。这也意味着,如果你有一个名单,其中,INT&GT;整数
,你想找到的第一个偶数,你可以这样做:
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);
当然,与任何其他类型的,你可以在code使用,这是给你的变量描述性的名称是个好主意;所以我会建议改变上述 pre
来像 evenFinder
或 ISEVEN
- 类似的规定。接着上面的code是一个更加清晰:
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);