Saturday, November 26, 2022

Lambda expression

good introduction on Lamda expression.

  1. Lambda expressions (C# reference)
  2. You use a lambda expression to create an anonymous function. Use the lambda declaration operator => to separate the lambda's parameter list from its body. A lambda expression can be of any of the following two forms:

    1. Expression lambda that has an expression as its body:
    2. (input-parameters) => expression

      You can use lambda expressions in any code that requires instances of delegate types or expression trees, for example as an argument to the Task.Run(Action) method to pass the code that should be executed in the background. You can also use lambda expressions when you write LINQ in C#, as the following example shows:

      Expression lambdas A lambda expression with an expression on the right side of the => operator is called an expression lambda. An expression lambda returns the result of the expression and takes the following basic form:

      (input-parameters) => expression

    3. Statement lambda that has a statement block as its body:
    4. (input-parameters) =>; { <sequence-of-statements > }

    5. Statement lambdas A statement lambda resembles an expression lambda except that its statements are enclosed in braces:

      (input-parameters) => { <sequence-of-statements > }

      The body of a statement lambda can consist of any number of statements; however, in practice there are typically no more than two or three.

    6. Input parameters of a lambda expression You enclose input parameters of a lambda expression in parentheses. Specify zero input parameters with empty parentheses:

      Action line = ( ) => Console.WriteLine();

      If a lambda expression has only one input parameter, parentheses are optional:

      Func <double, double > cube = x => x * x * x;

      Two or more input parameters are separated by commas:

      Func <int, int, bool > testForEquality = (x, y) => x == y;

      Sometimes the compiler can't infer the types of input parameters. You can specify the types explicitly as shown in the following example:

      Func <int, string, bool > isTooLong = (int x, string s) => s.Length > x;

      Input parameter types must be all explicit or all implicit; otherwise, a CS0748 compiler error occurs.

  3. => operator (C# reference)
  4. The => token is supported in two forms: as the lambda operator and as a separator of a member name and the member implementation in an expression body definition.

    Lambda operator In lambda expressions, the lambda operator => separates the input parameters on the left side from the lambda body on the right side.

    Expression body definition An expression body definition has the following general syntax:

    member => expression;

    Operator overloadability The => operator cannot be overloaded.

  5. Expression-bodied members (C# programming guide)
  6. Expression body definitions let you provide a member's implementation in a very concise, readable form. You can use an expression body definition whenever the logic for any supported member, such as a method or property, consists of a single expression. An expression body definition has the following general syntax:

    member => expression;

No comments:

Post a Comment