Lambda Expressions
Description
Lambda expressions is an anonymous method
- No access modifier
- No name
- No return statement
Basic format: args => expression
With no arguments: () => expression
With only 1 argument: x => expression
Multiple arguments: (x, y, z) => expression
static int Square(int number)
{
return number*number;
}
//could be rewritten using a delegate and lambda expressioon as follows:
Func<int,int> square = number => number*number;
A lambda expression scope extends to all arguments passed to the lambda expression and any variable defined in the same method.
const int factor = 5;
Func<int,int> mutiplier = n => n*factor;
var result = mutiplier(10);

could be written with lanbda operator:
