Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
i want know difference between Lambda expression and Action.?
Posted 2-Feb-11 2:05am
Comments
digital man at 2-Feb-11 8:33am
   
Try Googling.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 Smile | :) . 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
});
 
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
  Permalink  
v5
Comments
AspDotNetDev at 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!
SAKryukov at 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 R. Bihy at 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.
SAKryukov at 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 at 2-Feb-11 15:50pm
   
Good answer, nice and easy example, 5+
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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!
  Permalink  
Comments
Marcus Kramer at 2-Feb-11 8:59am
   
Great find. +5
Manfred R. Bihy at 2-Feb-11 9:19am
   
Thanks Marcus!
SAKryukov at 2-Feb-11 13:11pm
   
Please see my alternative. As the question is incorrect, it needs definitive answer on no answer at all.
--SA
SAKryukov at 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 at 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)

  Print Answers RSS
0 OriginalGriff 221
1 BillWoodruff 205
2 CPallini 199
3 Maciej Los 170
4 Kornfeld Eliyahu Peter 160


Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 2 Feb 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100