Lambda表情和动作之间的区别 [英] difference between Lambda expression and Action

查看:82
本文介绍了Lambda表情和动作之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道Lambda表达式和Action之间的区别.?

i want know difference between Lambda expression and Action.?

推荐答案

该问题没有任何意义,因为大多数问题都像有什么区别"? 苹果和苹果有什么区别?"是经典的:-).您可以问一下它们之间的关系.

既学习又看.

Lambda表达式是一种严肃的话题,请从此处开始: http://msdn.microsoft.com/en-us /library/bb397687.aspx [ ^ ].

动作太简单了.此ActionFunc是在System.Core中声明的委托类型声明的重载集.如果您知道委托类型和委托,则没有新内容.为了方便起见,Microsoft刚刚在.NET v.3.5中添加了一组最典型的通用委托类型.这些声明涵盖了大约99.99%的实际案例.

它们经常一起使用.此lambda表达式只是将匿名方法添加到某些委托的调用列表中的便捷方法.比较:

The question makes no sense, as most questions like "what''s the difference"? "What''s the difference between apple and Apple?" is the classic one :-). You could ask how are they related though.

Just learn both and see.

Lambda expression is kind of serious topic, start here: http://msdn.microsoft.com/en-us/library/bb397687.aspx[^].

Action it too simple. This Action and Func is overloaded set of delegate type declarations, declared in System.Core. If you know delegate types and delegates, there is nothing new. Microsoft just added a set of most typical generic delegate types for our convenience, in .NET v.3.5 I think. These declarations cover some 99.99% of practical cases.

They are often used together. This lambda expression is just a convenient way to add an anonymous method to the invocation list of some delegate. Compare:

Action<string, int> myDelegate;

//without lambda:
myDelegate = delegate(string message, int index) {
    //use message
    //use index
};

//using lambda, same thing (adding to already existing invocation list, hence +=):
myDelegate += new Action<string, int>((message, index) => {
    //use string message
    //use int index
});



在lambda形式中,方便之处在于messageindex的类型未明确键入,因为它们是从Action声明中推断出来的.这非常方便,尤其是在事件中,其中从事件类型中推断出sender和event参数参数(您甚至没有在Action中重复它们),因为通常甚至不使用一个或两个参数,因此查找确切类型声明可能纯粹是毫无目的的麻烦;类型推断在这里有帮助.

在功能上,两种方式都是100%相同.

lambda的使用有更多基本和重要的用途,但我只专注于将其与Action配合使用的方面,以回答此问题.

—SA



In the lambda form, the convenience is that the types of message and index are not typed explicitly as they are inferred from the Action declaration. This can be very convenient, especially in events, where sender and event arguments parameters are inferred from the event type (you don''t even repeat them in Action), because one or both parameters are often not even used, so finding exact type declarations could be a pure hassle with no purpose; type inference helps here.

Functionally, both ways are 100% identical.

There are much more fundamental and important uses of lambda, but I only focused on the aspect of using it with Action, to answer this question.

—SA


您好,请在此处查看此讨论: ^ ].这几乎为我清除了.

还不清楚吗?发表评论.

干杯!
Hi have a look at this discussion here: http://stackoverflow.com/questions/765966/what-is-the-difference-between-new-action-and-a-lambda[^]. That pretty much cleared it up for me.

Still unclear? Leave a comment.

Cheers!


这篇关于Lambda表情和动作之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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