Click here to Skip to main content
12,760,377 members (33,442 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# C#4.0
i want know difference between Lambda expression and Action.?
Posted 2-Feb-11 3:05am
digital man 2-Feb-11 8:33am
Try Googling.
Rate this: bad
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 :-). You could ask how are they related though.

Just learn both and see.

Lambda expression is kind of serious topic, start here:[^].

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.

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!
SAKryukov 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.
Espen Harlinn 2-Feb-11 15:50pm
Good answer, nice and easy example, 5+
Manfred R. 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.
SAKryukov 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.)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Hi have a look at this discussion here:[^]. That pretty much cleared it up for me.

Still unclear? Leave a comment.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 2 Feb 2011
Copyright © CodeProject, 1999-2017
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