Lambda de expresión frente a Lambda de declaración

Fundamentally, is there any difference between a single-line expression lambda and a statement lambda? Take the following code, for example:

private delegate void MyDelegate();

protected static void Main()
{
    MyDelegate myDelegate1 = () => Console.WriteLine("Test 1");
    MyDelegate myDelegate2 = () => { Console.WriteLine("Test 2"); };

    myDelegate1();
    myDelegate2();

    Console.ReadKey();
}

While I prefer the first because I find the brackets to be ugly, is there anything different between the two (besides the obvious part about requiring brackets for multi-line statements)?

preguntado el 09 de marzo de 12 a las 15:03

8 Respuestas

You need statement lambda for multistatement lambdas. In addition statement lambdas are not supported by expression providers like LINQ to SQL. Before .NET 4.0 the .NET Framework did not have support for statement expression trees. This was added in 4.0 but as far as I know no provider uses it.

Action myDelegate1 = () => Console.WriteLine("Test 1");
Expression<Action> myExpression = () => { Console.WriteLine("Test 2") }; //compile error unless you remove the { }
myDelegate1();
Action myDelegate2 = myExpression.Compile();
myDelegate2();

De lo contrario, son iguales.

respondido 09 mar '12, 15:03

Reflector to the rescue! The disassembled code looks like this:

private static void Main(string[] args)
{
    MyDelegate myDelegate1 = delegate {
        Console.WriteLine("Test 1");
    };
    MyDelegate myDelegate2 = delegate {
        Console.WriteLine("Test 2");
    };
    myDelegate1();
    myDelegate2();
    Console.ReadKey();
}

So no, there is no real difference between the two. Be happy.

respondido 09 mar '12, 15:03

The two are the same - the first is azúcar sintáctica to the second and both will compile to the same IL.

respondido 09 mar '12, 15:03

No, there is no difference in this example. If the body of the lambda is only one expression, you can drop the brackets. However, once the lambda contains more than one expression, like so:

MyDelegate myDelegate2 = () => { 
  Console.WriteLine("Test 2");                      
  Console.WriteLine("Test 2"); 
};

the brackets are mandatory.

respondido 09 mar '12, 15:03

If the delegate returns a value, return is necessary in statement lambda as follows.

        Func<int, int, bool> foo = (x, y) => { return x == y; };
        Func<int, int, bool> goo = (x, y) => x == y;

Respondido 18 ago 14, 18:08

Beside from multiple statements, presence of return keyword is also another fact. +1 - Wappenull

Personnaly, i prefer the Lambda Expression. The Expression have a value where the Statement does not.

i think the following links can help you :

http://lambda-the-ultimate.org/node/1044

respondido 09 mar '12, 15:03

Same for the OP example, but after C# 6.0, allowing you to use same expression syntax to define normal non-lambda methods within a class. For example:

public static double AreaOfTriangle(double itsbase, double itsheight)
{
    return itsbase * itsheight / 2;
}

The above code snippet can be written only if the method can be turned into a single expression. In short, it can be used the expression lambda syntax, but not the statement lambda syntax.

public static double 
              AreaOfTrianglex(double itsbase, double itsheight) => itsbase * itsheight / 2;

Respondido el 18 de Septiembre de 16 a las 15:09

De los documentos (https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions):

Statement lambdas, like anonymous methods, cannot be used to create expression trees.

contestado el 10 de mayo de 19 a las 00:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.