Lambda Expression is a short block of code that accepts parameters and returns a value.
Func<int,int> Square = x => x * x;
Square(5); // 25
Features
- Infer Natural delegate type for lambda and method groups
- Allow having attribute in lambdas
- Lambda with explicit Return type
Infer natural delegate type
Attributes on Lambda
Explicit return type
We can specify explicitly the return type of the lambda expression.
This feature can become interesting if the return type of the lambda expression can't be inferred because the lambda can return different type of results demo
Motivation
All those improvement on lambda expression made easier the introduction of Minimal API!
Minimal Api are a Asp.net feature that allows the creation of endpoints with minimal dependencies. Consider the following code using the classical API Controller demo
Now let's see how we can make the same with Minimal Api demo