Lambdas

Create anonymous methods using a more concise syntax.

Overview

Lambdas are a simpler way to declare anonymous methods, though the syntax might look a bit tricky at first.

Let’s look at an example. We’ll rewrite the code from the previous lesson:

C#
using System;
namespace Lambdas
{
delegate int MathOperation(int operand1, int operand2);
class Program
{
static void Main(string[] args)
{
// Lambda expression
MathOperation mathOperation = (operand1, operand2) => operand1 + operand2;
// Lambda expression
mathOperation += (x, y) => x - y;
Console.WriteLine(mathOperation(2, 3));
}
}
}

Syntax and usage

Let’s disassemble the lambda expression to understand its individual parts:

  • (operand1, operand2): These are the parameters of the anonymous method. The types of parameters are omitted because they’re retrieved implicitly from the delegate’s signature.
  • =>: This is the method body. The return keyword isn’t necessary. Unless a statement is an assignment or some action that doesn’t generate a value, the return keyword is injected and the value is returned. In our case, the result of operand1 + operand2 is returned.

If an anonymous method accepts a single parameter, parentheses around the parameters can be omitted:

C#
using System;
namespace Lambdas
{
delegate int IncrementOperation(int number);
class Program
{
static void Main(string[] args)
{
// No parentheses because there is only one parameter
IncrementOperation incrementor = x => x + 1;
Console.WriteLine(incrementor(5));
}
}
}

Empty parentheses are used if the lambda expression doesn’t accept any parameters. The following code also demonstrates a case when a lambda doesn’t return any value.

C#
using System;
namespace Lambdas
{
// Does not return anything
// Does not accept any parameters
delegate void MessagePrinter();
class Program
{
static void Main(string[] args)
{
// Empty parentheses if no parameters
// No return value
MessagePrinter printer = () => Console.WriteLine("Hello World!");
printer += () => Console.WriteLine("Hello once again!");
printer();
}
}
}

Notice that we don’t have to state the parameter types when declaring lambdas. However, if the delegate has parameters modified with the ref or out keywords, then we must provide the types as well:

C#
using System;
namespace Lambdas
{
// Delegate with a parameter decorated with ref
delegate void IncrementOperation(ref int number);
class Program
{
static void Main(string[] args)
{
// We must write the parameter types now
IncrementOperation incrementor = (ref int number) => number++;
int x = 10;
incrementor(ref x);
Console.WriteLine(x);
}
}
}

Lambdas as method parameters

Lambdas can serve as method parameters, just like delegates and anonymous methods. In the following code, we receive the same end result as the previous code playground, but with different implementation:

C#
using System;
namespace Lambdas
{
delegate void IncrementOperation(ref int number);
class Program
{
static void Main(string[] args)
{
int x = 10;
// We provide a lambda expression as a delegate parameter
ModifyNumber(ref x, (ref int number) => number++);
Console.WriteLine(x);
}
// This method accepts a delegate as a parameter
static void ModifyNumber(ref int number, IncrementOperation incrementor)
{
incrementor(ref number);
}
}
}