10

Duplicate: delegate keyword vs. lambda notation

I understand the anonymous methods can be used to define delegates and write inline functions. Is using Lambda expressions any different from this?

I guess I am a little confused on when to use what.

Edit: Also, appears that to use either anonymous or lambdas, there needs to be an Extension method for the type?

Community
  • 1
  • 1
Bob Smith
  • 349
  • 2
  • 5
  • 15

4 Answers4

16

A lambda expression is simply shortcut syntax for an anonymous method. Anonymous methods look like this:

delegate(params) {method body}

The equivalent lambda expression would look like this:

params => method body

In short, all lambda expressions are anonymous methods, but it is possible to have an anonymous method that is not written in lambda syntax (like the first example above). Hope this is helpful!

Adam Alexander
  • 15,132
  • 5
  • 42
  • 41
  • +1, but aren't lambda expressions just reinventing the wheel (rather poorly, as I though anonymous methods made more sense). – Steven Evers Apr 17 '09 at 17:10
  • so lambada expression can NOT have return valuess? while anonymous method can? –  Apr 17 '09 at 17:13
  • 1
    SnOrfus: yes they do the same thing so you're right, it's just a matter of syntax preference. Sasha: both lambdas and anonymous methods can have return values. – Adam Alexander Apr 17 '09 at 17:19
  • 1
    Lambdas make the use of anonymous delegates much more terse through syntax shortcuts and type inference. i = l.Count(delegate(string x) { return x.Length == 2; }); i = l.Count(x => x.Length == 2); – Mark Apr 17 '09 at 17:38
  • 4
    lambdas use implied typing, so "(x, y) => x + y;" is much more concise than "delegate(int x, int y) { return x + y; }" Use lambdas when they promote readability, and anonymous delegates when they make sense. – Michael Meadows Apr 17 '09 at 17:46
6

Only lambda expression without method body can be converted to expression tree

Following constructs do compile:

            Func<int> exp1 = () => 1;
            Func<int> exp2 = () => { return 1; };
            Func<int> exp3 = delegate { return 1; };
            Expression<Func<int>> exp4 = () => 1;

And following do not

Expression<Func<int>> exp5 = delegate { return 1; }; //no anonymous delegates
Expression<Func<int>> exp6 = () => { return 1; }; //or lambdas with block body

So there is difference even on not very advanced level ( that Jon Skeet points out here sick difference example )

Another difference is that you can create anonymous delegates without parameter list if you don't plan to use them inside method body, with lambda you always have to provide parameters.

Following two lines demonstrate the difference

Func<int, int, int, int, int> anonymous = delegate { return 1; };
Func<int, int, int, int, int> lambda = (param1, param2, param3, param4) => 1;

You do essentially the same thing but anonymous delegate clearly looks better here.

Community
  • 1
  • 1
Valentin Kuzub
  • 11,703
  • 7
  • 56
  • 93
6

Lambda expressions can be converted to expression trees, while anonymous delegates cannot.

bdukes
  • 152,002
  • 23
  • 148
  • 175
4

Not really no. They are essentially the exact same feature with different syntax constructs. The general shift appears to be away from the C# 2.0 anonymous method syntax towards the lambda style syntax for both anonymous expressions and functions though.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454