C# 12: Default Parameters in Lambdas

There is still a long road ahead of us until the release of .NET 8, but the first new language constructs are getting public. The first one I want to present is: Default Parameters in Lambdas.


Often times lambdas are equal to their big brothers and sisters aka functions. We can do the following function: int Increment(int value, int increment = 1); but that doesn't work with lambda expression - well until now. From C# 12 onwards you can do this:

var increment = (int value, int inc = 1) => value + inc;
Console.WriteLine(increment(10, 10));

That outputs 20 and 11.


A classic example where this can come in handy is Minimal API. Often times those functions are just lambdas and sometimes you want to have defaults (for example for paging):

app.MapGet("getblogposts", async (MyService service, int? page = 1, int? pageSize = 50) => {}

Try it out!

You can try it out on your own. For example on sharplab.io where I set up the latest compiler version. Alternatively you can download .NET 8 preview. To make use of the new feature you have to set the language version to preview. So a new project (console) might look like this:

<Project Sdk="Microsoft.NET.Sdk">




An error has occurred. This application may no longer respond until reloaded. Reload x