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">




Less boilerplate code with the new primary constructor in C# 12

The new language feature "primary constructor" which will be released with C# 12 / .NET 8 this year (November 2023) allows you to remove some ceremonial code. Let's see how.

C# 12: Collection literals

A new feature is hitting C# 12 and it is called Collection literals. In this blog post, I will show you what it is and how it works.

Avoid multiple boolean parameters

Boolean parameters are nice, but it's hard to keep track of what each one does when you have multiple of them. In this blog post, we will see why it's better to avoid multiple boolean parameters and how to refactor them.

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