.NET Lambda Expression Cheat Sheet
Table of Contents
Resources
A Simple Lambda Expression
A simple lambda expression looks like this:
x => x + x
=>
is called the lambda operator.- The left side of the lambda operator holds the parmeter / the parameters to the lambda expression.
- The right side of the lambda operator holds an expression or a statment block.
Parameters to a Lambda Expression
- Parameters to a lambda expression are on the left side of the lambda operator
- Multiple parameters have to be enclosed in parentheses
(x, y, z) => x + y + z;
- A single parameter can be written without parentheses
x => x + x
- For a paramterless lambda expression, only the parentheses have to written.
() => 5 + 5
Type Inferrence
- The rules for type inferrence apply to lambdas as to everywhere else in c#.
The following two examples are equal:
(x, y, z) => x + y + z;
(int x, int y, int z) => x + y + z;
- A single but explicitely typed parameter to a lambda also has to be enclosed in parentheses
(int x) => x + x
Make Use of a Lambda Expression with Delegates
- To make use of a lambda, it has to be assigned to a delegate.
delegate void MyDel (int x); ... MyDel lx = x => x + x; Console.WriteLine(lx(5));//10
- A lambda can also be used where a delegate is expected. In this case it will automatically be assigned to a delegate of the expected type.
In the following example the function DelegateCaller expects a delegate of type int(int).
using System; namespace LambdasDemo { class Program { delegate int IntDel(int i); static void Main(string[] args) { Console.WriteLine(DelegateCaller(x => x + x, 5)); //10 } static int DelegateCaller(IntDel del, int x) { return del(x); } } }
Statement Lambdas
- Statement lambdas hold a statement block on the right side of the lambda operator.
- The statement block has to be enclosed in curly brackets as in every other function in c#.
- To pass something back from the statement lambda to the caller, the return keyword is used (as in every other function in c#).
x => {int y = 2*x; return y;}
- If return is not used explicitely, the statement lambda implicitely returns void.
delegate void MyDel (int x); ... MyDel lx = x => {int y = 2*x; Console.WriteLine(y);};
Expression Lambdas
- Expression lambdas hold an expression on the right side of the lambda operator.
- Per definition an expression is everything, that returns something.
(int x) => x + x
… returns int.
(string name) => "Hello " + name + "!"
… returns a string
x => Console.Writeline(x)
… returns void (because Console.WriteLine(…) returns void).
- This way a statement block of a statement lambda can also be viewed as an expression. The statement block as a whole always returns something. Either somethding, that was passed explicitely or an implicitely passed void.
It Is Important to Know What a Lambda Returns and Why
The following expression lambda and statement lambda are equal:
(int x) => x + x
(int x) => {return x + x;}
… they both return int.
d => Math.Floor(d)
… returns int. Int is the return type of Math.Floor(…).
d => {Math.Floor(d);}
… returns void. Void is the implicit return type of the statement block.
d => {return Math.Floor(d);}
… returns int. Int is the explicit return type of the statement block.