Click here to Skip to main content
15,886,058 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want know difference between Lambda expression and Action.?
Posted
Comments
R. Giskard Reventlov 2-Feb-11 8:33am    
Try Googling.

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:

C#
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
});


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
 
Share this answer
 
v5
Comments
AspDotNetDev 2-Feb-11 13:55pm    
I had no idea you could use curly braces to create a code block that facilitates a multi-line lambda. Thanks for that insight!
Sergey Alexandrovich Kryukov 2-Feb-11 14:11pm    
You're very welcome!

Of course you can; I use it all the time with events, where it's the most convenient way to provide handlers. Another advice would be never using auto-generated (by visual designers) event handlers. If you refrain yourself from this type of "clicking" and switch to manual, the code becomes way more supportable. You need to try to see the benefit.

I would be happy if it helps you.
--SA
Manfred Rudolf Bihy 2-Feb-11 18:42pm    
Yes I use multi-line lambdas all the time as they're especially neat because one can use all the locally defined variables (and the globals too of course) where the anonymous method is defined. My favorite application for this is the definition of Predicate<>. methods.
Sergey Alexandrovich Kryukov 2-Feb-11 19:13pm    
Absolutely. And more. A secret hint: you can even develop Computer Algebra System using those.
(However, I successfully did it with v.2.0, before lambdas.)
--SA
Espen Harlinn 2-Feb-11 15:50pm    
Good answer, nice and easy example, 5+
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!
 
Share this answer
 
Comments
fjdiewornncalwe 2-Feb-11 8:59am    
Great find. +5
Manfred Rudolf Bihy 2-Feb-11 9:19am    
Thanks Marcus!
Sergey Alexandrovich Kryukov 2-Feb-11 13:11pm    
Please see my alternative. As the question is incorrect, it needs definitive answer on no answer at all.
--SA
Sergey Alexandrovich Kryukov 2-Feb-11 13:12pm    
May me a good reference (my 5), but as the question is incorrect... (see my comment to comment by Marcus), please see my answer.
--SA
Espen Harlinn 2-Feb-11 15:51pm    
Good link - 5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900