What is the difference between anonymous methods (C # 2.0) and lambda expressions (C # 3.0)?

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

+36
methods c # expression
Oct. 16 '08 at 12:38
source share
4 answers

Anonymous Methods MSDN Page Explains This

In C # versions prior to 2.0, the only way to declare a delegate was to use the named methods. Added C # 2.0 anonymous methods and in C # 3.0 and later lambda expressions replace anonymous methods as the preferred way to write inline code. However, information on anonymous methods in this topic also applies to lambda expressions. In one case, which anonymous method provides functionality is not found in the lambda expression. Anonymous methods allow you to omit the parameter list and this means that the anonymous method can be converted to delegates using many signatures. This is not possible with lambda expressions. For more information on 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 types of expressions tree. All lambda expressions use the lambda operator =>, which is read as "going". The left side of the lambda operator indicates the input parameters (if any), and the right side contains the block of the expression or operator. The lambda expression x => x * x reads "x goes x times x". This expression can be assigned to the delegate type as follows:

+27
Oct. 16 '08 at 12:41
source share
  • Anonymous methods are basically functions without a name, with the ability to create closures.
  • Lambda expressions are constructs that can be converted to both anonymous methods and expression trees, and follow more complex type inference rules than anonymous ones.

The range of more or less subtle differences is explained by Eric Lippert (the C # language developer) on his blog:

+21
Oct. 16 '08 at 12:59
source share
  • Lambda expressions can be converted to delegates or expression trees (with some restrictions); anonymous methods can only be converted to delegates
  • Lambda expressions allow you to display parameters by parameters:
  • Lambda expressions allow you to trim a body only for an expression (to return a value) or a single operator (in other cases) without curly braces.
  • Lambda expressions allow you to shorten the list of parameters to the parameter name, when the type can be inferred, and when there is only one parameter
  • Anonymous methods make it possible to completely exclude the parameter list when it is not used inside the body, and this does not lead to ambiguity.

The last question is the only advantage of anonymous methods over lambdas, I think. It is useful to create a semi-similar event with a no-op subscription, though:

public event EventHandler Click = delegate{}; 
+21
Oct. 16 '08 at 14:29
source share

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

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

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

Please note that lambda expressions with operator bodies can only be compiled by delegates, not Expression s:

 Action a = () => { Console.WriteLine(obj.ToString()); }; 
+10
Oct. 16 '08 at 12:50
source share



All Articles