What's the difference between anonymous methods (C# 2.0) and lambda expressions (C# 3.0)? [duplicate]
Asked Answered
C

4

47

What is the difference between anonymous methods of C# 2.0 and lambda expressions of C# 3.0.?

Centralia answered 16/10, 2008 at 12:38 Comment(0)
S
31

The MSDN page on anonymous methods explains it

In versions of C# before 2.0, the only way to declare a delegate was to use named methods. C# 2.0 introduced anonymous methods and in C# 3.0 and later, lambda expressions supersede anonymous methods as the preferred way to write inline code. However, the information about anonymous methods in this topic also applies to lambda expressions. There is one case in which an anonymous method provides functionality not found in lambda expressions. Anonymous methods enable you to omit the parameter list, and this means that an anonymous method can be converted to delegates with a variety of signatures. This is not possible with lambda expressions. For more information specifically about lambda expressions, see Lambda Expressions (C# Programming Guide).

And regarding lambda expressions:

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types. All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read "x goes to x times x." This expression can be assigned to a delegate type as follows:

Shushan answered 16/10, 2008 at 12:41 Comment(0)
C
32
  1. Lambda expressions can be converted to delegates or expression trees (with some restrictions); anonymous methods can only be converted to delegates
  2. Lambda expressions allow type inference on parameters:
  3. Lambda expressions allow the body to be truncated to just an expression (to return a value) or single statement (in other cases) without braces.
  4. Lambda expressions allow the parameter list to be shortened to just the parameter name when the type can be inferred and when there's only a single parameter
  5. Anonymous methods allow the parameter list to be omitted entirely when it's not used within the body and it doesn't lead to ambiguity

The last point is the only benefit of anonymous methods over lambdas, I believe. It's useful to create a field-like event with a no-op subscription though:

public event EventHandler Click = delegate{};
Cyprinoid answered 16/10, 2008 at 14:29 Comment(5)
"Lambda expressions allow the parameter list to be shortened to just the parameter name when the type can be inferred and when there's only a single parameter" - Multiple parameters are fine too, no? (x,y) => {}Danieu
@Basic: No, that's the parameter names in parentheses. Compare that with x => x.Length. Your example is from point 2, "Lambda expressions allow type inference on parameters".Cyprinoid
@JonSkeet Can you please elaborate the 5th point?Arcanum
@RichaGarg while defining an anonymous method being pointed by a delegate variable, you can omit the parameters while defining anonymous method's body if those parameters haven't been referred in the body of the anonymous method. Have a look at my code snippet here to get more clarity. This post precisely elaborates this scenario. Jon please correct me if I'm wrong.Dud
@RBT: Spot on. Thanks!Cyprinoid
S
31

The MSDN page on anonymous methods explains it

In versions of C# before 2.0, the only way to declare a delegate was to use named methods. C# 2.0 introduced anonymous methods and in C# 3.0 and later, lambda expressions supersede anonymous methods as the preferred way to write inline code. However, the information about anonymous methods in this topic also applies to lambda expressions. There is one case in which an anonymous method provides functionality not found in lambda expressions. Anonymous methods enable you to omit the parameter list, and this means that an anonymous method can be converted to delegates with a variety of signatures. This is not possible with lambda expressions. For more information specifically about lambda expressions, see Lambda Expressions (C# Programming Guide).

And regarding lambda expressions:

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types. All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read "x goes to x times x." This expression can be assigned to a delegate type as follows:

Shushan answered 16/10, 2008 at 12:41 Comment(0)
E
27
  1. Anonymous methods are basically functions without a name, with the ability to create closures.
  2. Lambda expressions are constructs that are convertible to both anonymous methods and expression trees, and follow more complex rules of type inference than anonymous methods.

The range of more or less subtle differences are explained by Eric Lippert (C# language designer) in his blog:

Esperance answered 16/10, 2008 at 12:59 Comment(0)
M
15

First, convenience: lambdas are easier to read and write.

Second, expressions: lambdas can be compiled to either a delegate, or an expression tree (Expression<T> for some delegate-type T, such as Func<int,bool>). Expression trees are the more exciting, as it is the key to LINQ to out-of-process data stores.

Func<int,bool> isEven = i => i % 2 == 0;
Expression<Func<int,bool>> isEven = i => i % 2 == 0;

Note that lambda expressions with statement bodies can only be compiled to delegates, not Expressions:

Action a = () => { Console.WriteLine(obj.ToString()); };
Masseuse answered 16/10, 2008 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.