Skip to content

Latest commit

 

History

History
47 lines (34 loc) · 1.82 KB

LambdaExpressionImprovements.md

File metadata and controls

47 lines (34 loc) · 1.82 KB

Lambda Expression Improvements

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
  • Prior to c# 10 every time we define a delegate we need to specify explicitly the delegate type demo
  • How much easier would be if we can just use var? demo
Attributes on Lambda
  • In version prior to c# 10, lambda didn't have the possibility to have attribute assigned to them demo
  • From c# 10, attributes can be used with Lambdas as well providing parity with Methods and Local Functions demo
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