Lambda Expression
C# 2.0 introduced anonymous methods, which we’ve just looked at. The syntax for anonymous methods, however, is somewhat verbose and requires information
that the compiler itself already knows. Rather than requiring you to include this redundant information, C# 3.0 introduced lambda expressions,
which pare down the syntax of anonymous methods.
In the anonymous method syntax, the delegate keyword is redundant because the compiler can already see that you’re assigning the method to a delegate. You can easily transform an anonymous method into a lambda expression by doing the following:
Change to following lambda expression
This simple transformation is less verbose and looks cleaner, but it only saves you six characters. There’s more, however, that the compiler can infer, allowing you to simplify the lambda expression further, as shown in the following code.
Some important points about lambda expression parameter lists are the following:
In the anonymous method syntax, the delegate keyword is redundant because the compiler can already see that you’re assigning the method to a delegate. You can easily transform an anonymous method into a lambda expression by doing the following:
- Delete the delegate keyword.
- Place the lambda operator, =>, between the parameter list and the body of the anonymous method. The lambda operator is read as "goes to".
operation myOperator = delegate(int param1,int param2) { Console.WriteLine(param1 + " + " + param2 + " = " + (param1 + param2)); };
Change to following lambda expression
operation myOperator = (int param1,int param2)=> { Console.WriteLine(param1 + " + " + param2 + " = " + (param1 + param2)); };
This simple transformation is less verbose and looks cleaner, but it only saves you six characters. There’s more, however, that the compiler can infer, allowing you to simplify the lambda expression further, as shown in the following code.
-
From the delegate’s declaration, the compiler also knows the types of the delegate’s parameters, so the lambda expression allows you to leave out the
parameter types.
- Parameters listed with their types are called explicitly typed.
- Those listed without their types are called implicitly typed.
- If there’s only a single, implicitly typed parameter, you can leave off the parentheses surrounding it.
- Finally, lambda expressions allow the body of the expression to be either a statement block or an expression. If the statement block contains a single return statement, you can replace the statement block with just the expression that follows the return keyword.
operation myOperator = (int param1,int param2)=> { Console.WriteLine(param1 + " + " + param2 + " = " + (param1 + param2)); }; operation myOperator2 = (param1, param2) => { Console.WriteLine(param1 + " + " + param2 + " = " + (param1 + param2)); }; operation myOperator3 = (param1, param2) => Console.WriteLine(param1 + " + " + param2 + " = " + (param1 + param2));
Some important points about lambda expression parameter lists are the following:
- The parameters in the parameter list of a lambda expression must match that of the delegate in number, type, and position.
-
The parameters in the parameter list of an expression do not have to include the type unless the delegate has either
ref
orout
parameters—in which case the types are required. - If there is only a single parameter and it is implicitly typed, the surrounding parentheses can be omitted. Otherwise, they are required.
- If there are no parameters, you must use an empty set of parentheses.
The term lambda expression comes from the lambda calculus, which was developed in the 1920s and 1930s by mathematician Alonzo Church and others.
The lambda calculus is a system for representing functions and uses the Greek letter lambda to represent a nameless function. More recently, functional
programming languages such as Lisp and its dialects use the term to represent expressions that can be used to directly describe the definition of a function,
rather than using a name for it.
0 comments:
Post a Comment