Lambdas
Write concise, inline functions using the lambda operator to simplify delegate instantiation.
Lambda expressions (or simply “lambdas”) are a concise way to represent anonymous methods. They allow us to write code that is shorter and often more readable than using the delegate keyword.
Creating a delegate with a lambda
Let’s rewrite the example from the previous lesson using a lambda expression.
Line 2: We define a lambda expression
(operand1, operand2) => operand1 + operand2. The compiler infers thatoperand1andoperand2are integers based on theMathOperationdelegate definition.Line 5: We add a second lambda
(x, y) => x - yto the delegate instance.Line 7: We invoke the delegate. As with anonymous methods,
mathOperationreturns the result of the last method in the list (2 - 3 = -1).
Syntax and usage
A lambda expression has two main parts, separated by the lambda operator => (read as “goes to”):
Input parameters on the left (if any).
Expression or statement block on the right.
In an expression lambda, like the one shown above, we omit the return keyword because the expression automatically returns its result.
Single parameter syntax
If a lambda expression has exactly one parameter, we can omit the parentheses around the parameter name.
Line 2: We define the lambda
x => x + 1. The compiler infersxis anint. Since there is only one parameter, we omit the parentheses().Line 4: We invoke the delegate with
5, returning6.
No parameters and no return value
If the lambda takes no parameters, we must use empty parentheses ().
Line 5: We use
()to indicate zero parameters.Line 5: The body is a single statement (
Console.WriteLine). Since the delegate returnsvoid, this is a valid expression lambda.Line 9: We invoke the delegate, executing both lambdas in order.
Explicit parameter types
The compiler usually infers parameter types automatically. However, if a delegate uses ref or out parameters, we must specify the types explicitly in the lambda.
Line 2: We define a lambda that accepts a
ref int. The bodynumber++increments the value.Line 5: We pass
xby reference. The lambda modifies the original variable.
Lambdas as method parameters
Just like anonymous methods, lambdas are frequently used as arguments for methods that accept delegates. This allows us to pass behavior, such as a calculation or specific action, directly into a method.
Line 4: We call
ModifyNumber. The second argument is a lambda expression(ref int number) => number++.Line 9: The
ModifyNumbermethod takes anIncrementOperationdelegate, which matches the signature of our lambda.Line 11: The method invokes the delegate, executing the logic defined in our lambda.
Lambdas vs. anonymous methods
While both features allow us to create inline methods, lambda expressions are the preferred modern syntax.
Feature | Lambda expression | Anonymous method |
Syntax | Concise ( | Verbose ( |
Type inference | Implicit types (usually) | Explicit types (usually) |
Parameter list | Required (except single param) | Can be omitted entirely |
Always use lambda expressions for new code. Use anonymous methods only when maintaining legacy code or if you specifically need to ignore all parameters using the delegate { } syntax.