C# Lambdas Part 1, a Quick Overview with Examples

Download full source code.

I have tried a few times to write a blog post about using C# lambdas, and each time I have been unable to finish it. Lambdas can be a little confusing at first, and as they get more complex, can be very confusing.

There isn’t one single way to write a lambda or use a lambda, and there isn’t one way to explain them either.

In the past, I have tried to find a way to explain it all in a few sentences, and that is why I couldn’t finish those posts.

In this post, I will show many examples of using lambdas with comments to explain what is going on.

The sample code uses Funcs and Actions, if you are not familiar with them check two posts I wrote on the topic - Actions and Funcs.

A quick overview

This may not be strictly correct, but it is how I understand them. If there is something egregiously wrong, let me know in the comments.

To my mind, a lambda is a way of writing a method, and just like a traditional method, it can take parameters (or none), and return any type (or none).

Why use a lambda?

A lambda can be invoked or passed to another method, where it will be invoked. That other method can pass parameters into the lambda.

This allows us, the program writers to decide how another method does something. Let me give a couple of concrete examples -

  1. Polly, the resilience framework lets you retry failed HttpClient requests. Of course Polly can not know what request you want to make, so you pass a Polly Retry method a lambda that makes a HTTP request. The Retry method invokes the lambda you wrote and builds in all the resilience code necessary to retry the request.

    var httpResponseMessage = await retryPolicy.ExecuteAsync(() => httpClient.GetAsync("/"));

  2. If you have a list of numbers and you want to use a LINQ Where on the list, the Where method can take a lambda that YOU define to return only the numbers you want. You could instead write a traditional method to pass to the Where method, but that would be more code than the lambda, and may be less readable.

int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var evenNumbersStatementLambda = 
    numbers.Where( // Where is a LINQ method that takes a Func<int, bool> to perform the filter.
        (numFromNumbers) => // The lambda is passed a parameter of type int by the Where method.
            { // this is called a statement lambda, the statement(s) to the right of => are enclosed in braces and has an explicit return statement.
                return numFromNumbers % 2 == 0; // The lambda returns true if the number is even.
            } // this lambda is executed for each number in the numbers list. 
        );

var evenNumbersExpressionLambda = numbers.Where( // the lambda can be written more succinctly.
        numFromNumbers =>  // this is an expression lambda, the statement to the right of => is not enclosed in braces. An explicit return statement is not used. 
        numFromNumbers % 2 == 0 // an explicit return is not needed
    );

More examples

There are many ways to work with and use lambdas. The rest of this post is sample code, with detailed comments explaining what each line does (I don’t repeat comments if the code is identical to something already explained).

I strongly encourage you to step through the code line by line to see what is being executed, what parameters are being passed, and what is being returned.

A .NET Fiddle of the code is available here.

I know the comments are a little difficult to read in this blog post, but please download the attached zip file or open the .NET Fiddle.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// For all these examples, step through the code and see what/how the code is executed.

// *** Common use case
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// this is a common use case for a lambda.
// A array of numbers is filtered using a lambda in LINQ where clause.

// this is it as a one liner
var evenNumbersShorterLambda = numbers.Where(numFromNumbers => numFromNumbers % 2 == 0);

var evenNumbers = numbers.Where( // Where takes a Func that has a int as a parameter and returns a bool - Func<int, bool> predicate
    (numFromNumbers) => // numFromNumbers is the parameter of the lambda, the Where will pass each number from the array of numbers to the lambda one at a time
        {
            return numFromNumbers % 2 == 0; // the lambda will return true if the number is even
        }
    ).ToArray(); // forces execution of the lambda


// *** Example 1 *** //
Action<string> writeHelloFriend = // Action that takes a single string as a parameter. Actions return nothing.
    (friend) => // this is the start of the lambda. (friend) is the parameter that will be passed to the lambda.
        Console.WriteLine($"Hello {friend}"); // This is the an expression lambda, it uses the friend parameter and prints to the command line. It returns nothing.

writeHelloFriend("Dave"); // Invoke the action, passing "Dave" as the parameter. This will execute the lambda above. "Dave" will be assigned to the friend parameter in the lambda.


// *** Example 2 *** //
Action<string> writeHelloFriend2 =
    (friend) =>
    {
        Console.WriteLine($"Hello {friend}"); // this is the same as the above, but with a statement lambda. Note the curly braces.
    };
writeHelloFriend2("Dave");


// *** Example 3 *** //
Func<string> sayHello = // sayHello is a Func that returns a string.
    () => // the lambda starts here, it takes no parameters, that is what the () means.
        "Hello"; // this is the body of the lambda. It returns "Hello". An explicit return is not required for expression lambdas.

// Console.WriteLine(..) has many overloads, but one is for a string.
// Calling sayHello() will return a string - "Hello".
Console.WriteLine(sayHello());


// *** Example 4 *** //
Func<string> sayHello2 =
    () =>
        { // this is the body of a statement lambda. It can have as many statements as needed. But it must have an explicit return (because the Func is defined that way). 
            return "Hello";
        }; // and it must be enclosed in curly braces.
Console.WriteLine(sayHello2());


// *** Example 5  *** //
Func<string, string> helloFriend = // helloName is a Func that takes a string parameter and returns a string.
    (friend) => // (friend) is the name of the parameter that will be passed to the lambda.
            $"Hello {friend}"; // this is the body of the lambda. It returns "Hello {friend}".
Console.WriteLine(helloFriend("Dave")); // invoke the Func, passing "Dave" as the parameter.


// *** Example 6 *** //
Func<int, int, string> add = // Add is a Func that takes two int parameters and returns an string.
    (x, y) =>  // (x, y) are the parameters that will be passed to the lambda, but they haven't been passed yet.
        $"Result of x + y ({x} + {y}) is {x + y}"; // this is the body of the lambda. It will use the x and y parameters, and returns a string.
Console.WriteLine(add(2, 3)); // invoke the Func, passing 2 and 3 as the parameters.

// If you were to think of the Func as a method instead, it would be:    
//          string AddTwoNumbers(int x, int y)
// Just like in a lambda, the x and y are the names of parameters that have not been passed yet. That is done by the caller.
// And they haven't been used yet, that is done in the body of the method, same in the lambda.
// Console.WriteLine(AddTwoNumbers(2, 3)); 

// *** Example 7 *** //
Func<int, int, string> multiply =
    (x, y) =>
        $"My multiply Func does this: x * y ({x} * {y}) is {x * y}";
// PerformCalculation(..) takes a Func that takes two int parameters and returns a string, PerformCalculation(..) also takes two int parameters.
string result1 = PerformCalculation(multiply, 4, 5); // the multiply Func is passed to PerformCalculation along with two parameters that will become the x and y parameters in the lambda body, and the result is assigned to result1.
Console.WriteLine(result1);


// *** Example 8 *** //
// This is the same as the previous example, but the lambda expression is written directly inside PerformCalculation().., satisfying the Func<int, int, string> parameter of PerformCalculation(..).
string result2 =
    PerformCalculation( // call to PerformCalculation
        (a, b) => // first parameter PerformCalculation expects is a Func that takes two int parameters... 
                $"Result of a * b ({a} * {b}) is {a * b}",  // ...and returns a string.
            3, 4);  // these correspond to the a and b in the parameters of PerformCalculation and will be passed to the lambda body where they become the a and b parameters.
Console.WriteLine(result2);


// *** Example 9 *** //
// This is the same as the previous example, but the lambda does not make use of the a and b parameters.
string result3 =
    PerformCalculation(
        (a, b) =>
                $"Not going to calculate anything",
            5, 6);  // these correspond to the a and b in the parameters of PerformCalculation, they have to be passed, but the lambda doesn't have to use them.
Console.WriteLine(result3);


// *** Example 10 *** //
// This is the same as the previous example but discarding parameters that are not going to be used. That is what the _ means
string result4 =
    PerformCalculation( // call to PerformCalculation
        (_, _) => // discard the parameters I'm not going to use. 
                $"Discarding the parameters",  // Return a string.
            default(int), default(int));
Console.WriteLine(result4);


// *** Local methods *** //
// PerformCalculation is a local method that takes a Func that takes two int parameters and returns a string, it also takes two more ints.
string PerformCalculation(Func<int, int, string> calculation, int a, int b)
{
    // The ints, a and b, are passed to the calculation Func. If you F11 through this, you will see the code in the lambda above being executed.
    return $"The lambda is called in PerformCalculation. {calculation(a, b)}";
}

I will write a follow up post with a few more examples of lambdas that are a little more complicated.

Download full source code.

comments powered by Disqus

Related