C#3.0 [英] C# 3.0

查看:69
本文介绍了C#3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是Predicate Deligate。请解释。

What is a "Predicate Deligate". Please explain.

推荐答案

如果通过谓词委托你的意思是一个Predicate< T>,那么这只是表达一个逻辑测试的
a方式(并且是2.0,而不是3.0 - 虽然3.0

可以产生一个谓词来自一个表达式树。


它有签名bool Predicate< T>(T值),意思是给出了

类型的对象T,该方法返回true或false - 对于

过滤器等有用。


此上下文中的委托(实例)一如​​既往地是一个

方法 - 指向这种方法的指针 - 即


public static bool IsEven(int value){

返回值% 2 == 0;

}


然后获得委托实例:

Predicate< TisEven = IsEven; //或者,=新谓词< T>(IsEven)在

中手写


然后你可以评价

bool fiveIsEven = isEven(5);


当然 - 实际上你不会这样做; -p谓词是有用的

当执行表达式的组件时(不是在C#3.0

意义上......)不可能知道表达本身 - 即List< T>

不能知道我感兴趣的是偶数...


然而,你不需要这个...你可以更简洁;所以,如果我有
有一个List< int>,我可以使用:

List< intevenList = greaterList.FindAll(IsEven);

或者使用匿名语法:

List< intevenList = greaterList.FindAll(delegate(int value){return

value%2 == 0;});


C#3.0引入了一个更简单的lambda。表达这一点的方式:


列表< intevenList = greaterList.FindAll(v = v%2 == 0);


这有用吗任何?
If by "predicate delegate" you mean a Predicate<T>, then this is just
a way of expressing a logical test (and is 2.0, not 3.0 - although 3.0
can produce a predicate from an expression-tree).

It has the signature "bool Predicate<T>(T value)", meaning that given
an object of type T, the method returns true or false - useful for
filters etc.

A delegate (instance) in this context is, as always, an instance of a
method-pointer to such a method - i.e.

public static bool IsEven(int value) {
return value % 2 == 0;
}

then to get a delegate instance:
Predicate<TisEven = IsEven; // or, = new Predicate<T>(IsEven) in
longhand

and then you can evaluate
bool fiveIsEven = isEven(5);

Of course - in reality you wouldn''t do this ;-p Predicates are useful
when the component executing the expression (not in the C#3.0
sense...) cannot possibly know the expression itself - i.e. List<T>
can''t know that I am interested in even numbers...

However, you don''t need this... you can be a lot more terse; so if I
have a List<int>, I can use either:
List<intevenList = biggerList.FindAll(IsEven);
or alternatively using anonymous syntax:
List<intevenList = biggerList.FindAll(delegate (int value) {return
value % 2 == 0;});

C# 3.0 introduces an easier "lambda" way of expressing this:

List<intevenList = biggerList.FindAll(v =v % 2 == 0);

Does that help any?


哎呀:

Predicate< TisEven = IsEven;

应该读过

Predicate< intisEven = IsEven;


Marc
Oops:
Predicate<TisEven = IsEven;
should have read
Predicate<intisEven = IsEven;

Marc


在匿名表达式中或在Lambda表达式中谓词委托?

" RITESHIR" < RI ****** @ discuss.microsoft.com写信息

新闻:B8 ********************** ************ @ microsof t.com ...
Predicate delegate as in anonymous expression or as in Lambda expression?
"RITESHIR" <RI******@discussions.microsoft.comwrote in message
news:B8**********************************@microsof t.com...

什么是Predicate Deligate。请解释。
What is a "Predicate Deligate". Please explain.



这篇关于C#3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆