What is Lambda Expression?

Lambda expression is one of the features introduced in the C# 3.0. Lambda expression helps you to ease the burden of writing verbose Anonymous Methods. It is a new way to write anonymous method. Actually during compile time all lambda expressions are converted in anonymous method.

First of all this is the introduction of anonymous method for those who are not aware with anonymous method feature.

Anonymous Method

Anonymous method feature was introduced in C# 2.0. Anonymous methods are used to write methods inline without declaring a formal method. Normally anonymous methods are used for small methods which are not reused. Anonymous methods let you declare a method body without giving it a name. Behind the scenes, they exist as 'normal' methods; however, there is no way to explicitly call them in your code. Anonymous methods can only be created when using delegates and, in fact, they are created using the delegate keyword.

Let's have an example:

We have a function GetAgeList which returns age list having ages greater than equal to 22. Find function is used to search ages greater than equal to 22.

public List <int> GetAgeList()
{
    List<int> ageList = new List<int>();
    ageList.Add(20);
    ageList.Add(24);
    ageList.Add(22);
    ageList.Add(26);
    return ageList.Where(Find).ToList();
}
public static bool Find(int age)
{
    return (age >= 22);
}

You can perform this task by an anonymous method like:

public List<int> GetAgeList()
{
    List<int> ageList = new List<int>();
    ageList.Add(20);
    ageList.Add(24);
    ageList.Add(22);
    ageList.Add(26);
    return ageList.Where(delegate(int age)
        {
            return (age >= 22);
        }).ToList();
}

You don't need to create function "Find" which will be used only one time. Anonymous method saves some typing work.

Now we will see how Lambda Expression makes this task easier.


Lambda Expressions

Lambda Expressions makes the thing even easier by allowing you to avoid writing anonymous method and statement block. Lambda Expressions are just a new way to write anonymous methods. At compile time all the lambda expressions are converted into anonymous methods according to lambda expression conversion rules.

Syntax

(parameters) => expression or statement block

The left side of the lambda operator "=>" represents the arguments to the method and the right side is the method body.


Different Types of Lambda Expressions

1. Expression Lambda

Expression lambda has only an expression on the right side of the lambda operator "=>"

(input parameters) => expression

num => num * num; //num is an integer and result of this expression will be num * num

2. Statement Lambda

Statement lambda has a statement block on the right side of the lambda operator "=>"

(input parameters) => {statement;}

num => { return num * num; }; //num is an integer and result of this expression will be num * num

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.


For the age list example the code using lambda expression will be written like:

public List<int> GetAgeList()
{
    List<int> ageList = new List<int>();
    ageList.Add(20);
    ageList.Add(24);
    ageList.Add(22);
    ageList.Add(26);
    return ageList.Where(age => age >= 22).ToList();
}

Advantages of Lambda Expression

  1. It reduces typing effort. We don't need to specify the name of the function, its return type, and its access modifier.
  2. When reading the code you don't need to find for the method's definition.

Lambda expressions should be short. A complex definition makes the calling code difficult to read.


Some Important Points about Lambda Expression

  1. Variables defined within a lambda expression are accessible only within the scope of the lambda expression body.
  2. Jump statements (break, goto, continue) are not allowed within anonymous method/lambda expression.
  3. Lambda expressions themselves do not have type.
  4. As lambda expressions do not have any type so lambda expression cannot be assigned to an implicitly typed local variable.