lambda表达式和匿名委托之间的真正区别 [英] The real difference between lambda expression and anonymous delegates

查看:138
本文介绍了lambda表达式和匿名委托之间的真正区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

任何人都可以向我解释(除了语法)

lambda表达式对匿名委托的真正优势吗?

Hi There,
Can anyone explain me the real advantages of (other than syntax)
lambda expressions over anonymous delegates?


>从我尝试过的以下示例中,我无法得出任何真正的
>From the following example that I tried, I couldn''t derive any real



一个优于另一个的优势。


委托int F(int a);


F fLambda = a = a ++;

F fAnonymous = delegate(int a){return a ++; };

fLambda(1);

fAnonymous(1);


谢谢,

Matt

advantage for one over the other.

delegate int F(int a);

F fLambda = a =a++;
F fAnonymous = delegate(int a) { return a++; };
fLambda(1);
fAnonymous(1);

Thanks,
Matt

推荐答案

我喜欢Lambda,因为它可以在打印时节省时间,打字和树木。

" ;太" < mu **************** @ gmail.com写信息

新闻:11 ************** ********@e9g2000prf.googlegro ups.com ...
I like the Lambda because it saves time, typing and trees when printing.
"Matt" <mu****************@gmail.comwrote in message
news:11**********************@e9g2000prf.googlegro ups.com...

你好,

任何人都可以解释我的(除了语法)

lambda表达式对匿名委托的真正优势?
Hi There,
Can anyone explain me the real advantages of (other than syntax)
lambda expressions over anonymous delegates?

>>从我试过的下面的例子中,我无法得到任何真正的
>>From the following example that I tried, I couldn''t derive any real


$ b一个优于另一个的$ b优势。


委托int F(int a);


F fLambda = a = a ++;

F fAnonymous = delegate(int a){return a ++; };

fLambda(1);

fAnonymous(1);


谢谢,

Matt

advantage for one over the other.

delegate int F(int a);

F fLambda = a =a++;
F fAnonymous = delegate(int a) { return a++; };
fLambda(1);
fAnonymous(1);

Thanks,
Matt



Matt写道:
Matt wrote:

你好,

有谁能解释一下(除语法之外)的真正优势

lambda表达式对匿名代表的影响?
Hi There,
Can anyone explain me the real advantages of (other than syntax)
lambda expressions over anonymous delegates?

>>从我试过的下面的例子中,我无法得到任何真正的
>>From the following example that I tried, I couldn''t derive any real


$ b一个优于另一个的$ b优势。


委托int F(int a);


F fLambda = a = a ++;

F fAnonymous = delegate(int a){return a ++; };

fLambda(1);

fAnonymous(1);


谢谢,

Matt

advantage for one over the other.

delegate int F(int a);

F fLambda = a =a++;
F fAnonymous = delegate(int a) { return a++; };
fLambda(1);
fAnonymous(1);

Thanks,
Matt



Hi Matt,


功能:无。它真的只是语法糖。但是,在代码中使用这两种方法都有很好的优势。


例如,当
$时,lambda表达式启用简洁,小巧且可读的代码b $ b小表达式正在表达错误。此外,它在功能和命令/过程编程之间提供了一个很好的

桥梁。


然而,当你有一个非常复杂的任务要执行时,它可能是更聪明

和/或使用代表所必需的。


您的问题是lambda表达式优于匿名的优势
$ b但是,$ b代表,如果你问到匿名

代表对lambda表达式的优势是什么,我会说他们允许更多编程

构造到进入你的匿名例程(例如作用域变量

声明,多个语句执行)并在函数中将其表示为函数




-

Tom Spink

爱丁堡大学

Hi Matt,

Functionally: none. It really is just syntactic sugar. However, there
advantages to using either approach in your code.

For example, lambda expressions enable concise, small and readable code when
small expressions are being, err, expressed. Also, it provides a nice
bridge between functional and imperative/procedural programming.

However, when you have a really complex task to perform, it may be wiser
and/or necessary to use a delegate.

Your question was what''s the advantage of lambda expressions over anonymous
delegates, however, if you asked what''s the advantage of anonymous
delegates over lambda expressions, I''d say that they allow more programming
constructs to go in your anonymous routine (such as scoped variable
declaration, multiple statement execution) and represent this as a function
within a function.

--
Tom Spink
University of Edinburgh


Tom,


Lambda表达式在功能上实际上与代表不同。

使用lambda表达式,你可以这样做:


表达式<谓词< int> e = i = i 5;


您不能使用委托(将其分配给表达式< T>)。当您编译此代码时,编译器会创建一个表达式树,其中可以访问

模型,该模型提供表达式的详细信息。

您可以通过调用表达式将表达式转换为委托:

Predicate< intgreaterThanFive = e.Compile();


当然,如果将lamba表达式赋给委托,

编译器会创建一个匿名委托。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse。 com

" Tom Spink" < ts **** @ gmail.com写信息

新闻:%2 **************** @ TK2MSFTNGP03.phx.gbl ...
Tom,

Lambda Expressions actually are different functionally than delegates.
With lambda expressions, you can do this:

Expression<Predicate<int>e = i =i 5;

You can''t do that with a delegate (assign it to an Expression<T>). When
you compile this code, the compiler creates an expression tree, where the
model can be accessed which gives the details of the expression.

You can convert the expression to a delegate by calling:

Predicate<intgreaterThanFive = e.Compile();

Of course, if you assign the lamba expression to a delegate, the
compiler creates an anonymous delegate all the same.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Tom Spink" <ts****@gmail.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...

Matt写道:
Matt wrote:

>你好,
任何人都可以解释我真实的(除语法之外)
lambda表达式优于匿名委托的优势?
>Hi There,
Can anyone explain me the real advantages of (other than syntax)
lambda expressions over anonymous delegates?

>>>从我试过的下面的例子中,我无法得到任何真正的
>>>From the following example that I tried, I couldn''t derive any real


一个优于另一个的优势。

委托int F(int a);

F fLambda = a = a ++;
F fAnonymous = delegate(int a) {return a ++; };
fLambda(1);
fAnonymous(1);

谢谢,
Matt

advantage for one over the other.

delegate int F(int a);

F fLambda = a =a++;
F fAnonymous = delegate(int a) { return a++; };
fLambda(1);
fAnonymous(1);

Thanks,
Matt



嗨马特,


功能:无。它真的只是语法糖。但是,在代码中使用这两种方法都有很好的优势。


例如,lambda表达式可以实现简洁,小巧且可读的代码



小表达式,错误,表达。此外,它在功能和命令/过程编程之间提供了一个很好的

桥梁。


然而,当你有一个非常复杂的任务要执行时,它可能是更聪明

和/或使用代表所必需的。


您的问题是lambda表达式优于
$ b $的优势b匿名

代表,但是,如果你问到匿名

代表对lambda表达式的优势是什么,我会说他们允许更多

编程

构造进入你的匿名例程(例如作用域变量

声明,多个语句执行)并将其表示为

函数

函数内。


-

Tom Spink

大学爱丁堡


Hi Matt,

Functionally: none. It really is just syntactic sugar. However, there
advantages to using either approach in your code.

For example, lambda expressions enable concise, small and readable code
when
small expressions are being, err, expressed. Also, it provides a nice
bridge between functional and imperative/procedural programming.

However, when you have a really complex task to perform, it may be wiser
and/or necessary to use a delegate.

Your question was what''s the advantage of lambda expressions over
anonymous
delegates, however, if you asked what''s the advantage of anonymous
delegates over lambda expressions, I''d say that they allow more
programming
constructs to go in your anonymous routine (such as scoped variable
declaration, multiple statement execution) and represent this as a
function
within a function.

--
Tom Spink
University of Edinburgh


这篇关于lambda表达式和匿名委托之间的真正区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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