Concept Flow - Expression-bodied lambdas
Define lambda with => expression
Invoke lambda
Evaluate expression
Return result
An expression-bodied lambda uses => to define a function with a single expression that returns a value immediately.
Func<int, int> square = x => x * x; int result = square(3); Console.WriteLine(result);
| Step | Action | Expression Evaluated | Result |
|---|---|---|---|
| 1 | Define lambda 'square' as x => x * x | x * x | Lambda stored, no output |
| 2 | Call square(3) | 3 * 3 | 9 |
| 3 | Print result | Console.WriteLine(9) | Outputs 9 |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| square | null | lambda function (x => x * x) | lambda function | lambda function |
| result | undefined | undefined | 9 | 9 |
Expression-bodied lambdas use => followed by a single expression. They automatically return the expression's value. No curly braces or return keyword needed. Example: Func<int,int> square = x => x * x; Call like: square(3) returns 9.