Lambda expressions allow you to define simple, anonymous methods—that is, methods that do not have names and that are defined where they are assigned to a delegate or passed to a delegate parameter. In many cases, working with lambda expressions can reduce the size of your code and the complexity of working with delegates. As you’ll see in later examples, lambda expressions are particularly powerful when combined with the where
clause in LINQ queries. Figure 21.7 reimplements the example of Fig. 21.6 using lambda expressions rather than explicitly declared methods IsEven
, IsOdd
and IsOver5
.
A lambda expression (line 17)
number => number % 2 == 0
begins with a parameter list (number
in this case). The parameter list is followed by the =>
lambda operator (read as “goes to”) and an expression that represents the lambda’s body. The lambda expression in line 17 uses the %
operator to determine whether the parameter’s number
value is an even int
. The value produced by the expression—true
if the int
is even, false
otherwise—is implicitly returned by the lambda expression. The lambda in line 17 is called an expression lambda, because it has a single expression to the right of the lambda operator. Note that we do not specify a return type for the lambda expression— the return type is inferred from the return value or, in some cases, from a delegate’s return type. The lambda expression in line 17 is equivalent to the IsEven
method in Fig. 21.6. Note that C# 6’s expression-bodied methods use a similar syntax to expression lambdas, including the lambda operator (=>).
In line 17 of Fig. 21.7, the lambda expression is assigned to a variable of type NumberPredicate
—the delegate type declared in line 9. A delegate can hold a reference to a lambda expression. As with traditional methods, a method defined by a lambda expression must have a signature that’s compatible with the delegate type. The NumberPredicate
delegate can hold a reference to any method that receives an int
and returns a bool
. Based on this, the compiler is able to infer that the lambda expression in line 17 defines a method that implicitly takes an int
as an argument and returns the bool
result of the expression in its body.
Lines 20–21 display the result of calling the lambda expression defined in line 17. The lambda expression is called via the variable that references it (evenPredicate
). Line 24 passes evenPredicate
to method FilterArray
(lines 49–66), which is identical to the method used in Fig. 21.6—it uses the NumberPredicate
delegate to determine whether an array element should be included in the result. Lines 27–28 display the filtered results.
Lambda expressions often are used as arguments to methods with delegate-type parameters, rather than defining and referencing a separate method or defining a delegate variable that references a lambda. Lines 32–33 select the odd array elements with the lambda
(int number) => number % 2 == 1
In this case, the lambda is passed directly to method FilterArray
and is implicitly stored in the NumberPredicate
delegate parameter.
The lambda expression’s input parameter number
is explicitly typed as an int
here— sometimes this is necessary to avoid ambiguity that would lead to compilation errors (though that is not the case here). When specifying a lambda parameter’s type and/or when a lambda has more than one parameter, you must enclose the parameter list in parentheses as in line 33. The lambda expression in line 33 is equivalent to the IsOdd
method defined in Fig. 21.6. Lines 36–37 of Fig. 21.7 display the filtered results.
Lines 40–41 use the lambda
number => {return number > 5;}
to find the int
s greater than 5
in the array and store the results. This lambda is equivalent to the IsOver5
method in Fig. 21.6.
The preceding lambda is called a statement lambda, because it contains a statement block—one or more statements enclosed in braces ({}
)—to the right of the lambda operator. This lambda’s signature is compatible with the NumberPredicate
delegate, because the parameter number
’s type is inferred to be int
and the statement in the lambda returns a bool
. For additional information on lambdas, visit
https://msdn.microsoft.com/library/bb397687