Lambda Expression in c# 3.0

This article will explain about Lambda expression and its usage. This article will also give differences between Lambda expression and Anonymous method. This article will talk about type inference in Lambda expression. This will also talk about delegate type conversion of lambda expression.

We will start with a code. This is a very simple code which is printing Hello World. But this is done with help of Lambda Expression.

image1.gif

  1. Lambda Expression provides a concise functional syntax for writing anonymous method.
  2. It is a type safe way to write functions that can be passed as argument for subsequent evaluation.
  3. Lambda expression are superset of anonymous method.
  4. It can contain either and statement or an expression.
  5. It may have or may not have parameters.
  6. Its parameter could be implicitly or explicitly type.

    image2.gif
  7. It uses the lambda operator. => This operator is known as GOES TO 

    image3.gif
     
  8. Different lambda expressions are

    image4.gif
     
  9. There are two types of Lambda expression

    image5.gif

    Expression Lambda

    A lambda expression, which is having expression at right hand side is called Expression lambda.

    image6.gif

    Examples:

    image7.gif

    Statement Lambda

    A lambda expression, which is having statement at right hand side is called Statement lambda.

    image8.gif

    Examples:

    image9.gif

Parameter types in Lambda Expression

image10.gif

  1. For explicit type parameter, each parameter does have their type.

    image11.gif
     
  2. For implicit type parameter, type of parameter is inferred from compatible delegate type.

    image12.gif

Working Example

See, the below code. How lambda expression is being used to find out name of the authors having more than 100 articles.

image13.gif

Output

image14.gif

Lambda Expression conversion to Compatible delegate type
Lambda expression is considered as a value, which is having special conversion rules. The Lambda expression value does not have a type but can be converted to compatible delegate type.

Let us suppose,

D-> Delegate

L-> Lambda Expression

Then D and L are called compatible if

  1. D and L have same number of parameters.
  2. If L has explicit type parameter then L should also have same type parameter list.
  3. If L has implicit type parameter then L should not have Ref and Out parameters.
  4. If D has a void return type and L is an expression type lambda expression then body of L is a valid expression and considered as a statement expression.
  5. If D has a void return type and L is a statement type lambda expression then body of L is a valid statement block where no expression contains return statement.
  6. If D is returning some type and L is an expression type lambda expression then body of L is a valid expression, if that is implicitly convertible to the return type of D.
  7. If D is returning some type and L is a statement lambda expression then body of L is a valid statement body if in which each Return statement specifies the type which could be implicitly convertible to the return type of D.

Example
Let us suppose, there is a delegate

delegate void MyDelegate(int num);

Below lambda expression would work fine due to rule 5 stated above.


MyDelegate r = (num) => Console.WriteLine(num) ;

Below lambda expression would not work fine due to failure of rule 5 stated above.

image15.gif

Lambda Expression and Anonymous method

  1. Lambda Expression permits parameter type to be omitted and inferred whereas anonymous method requires parameter type to be explicitly stated.
  2. The body of lambda expression can be expression or statement whereas body of anonymous method only could be statement.
  3. Lambda Expression within an expression body can be converted to expression tree.

Type inference in Lambda Expression

Let, we suppose we are assigning a value to a variable like below. Compiler will report an error "Cannot assign lambda expression to an implicitly typed local variable". Because c# type inference cannot be used to infer to infer type of a lambda expression.

image16.gif

This could be solved by, explicitly declaring the type of the lambda expression.

Func<int> res = () => 9;

For inferred return type of a lambda expression L is determined as follows:

  • If L is an expression type Lambda Expression, the type of that expression is the inferred return type of L.
  • If L is a statement type Lambda Expression, if the set formed by the types of the expressions in the block's return statements contains exactly one type to which each type in the set is implicitly convertible, and if that type is not the null type, then that type is the inferred return type of L.
  • Otherwise, a return type cannot be inferred for L.

Up Next
    Ebook Download
    View all
    Learn
    View all