---
title: "C# 12: Default Parameters in Lambdas"
description: "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**."
updated: 2022-12-30
tags: [".NET", ".NET 8", "C# 12", "C#"]
canonical: "https://steven-giesel.com/blogPost/79ce95ad-17a9-4537-8f1b-48d71efc585d/c-12-default-parameters-in-lambdas"
---

# 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**.

## Defaults
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:
```csharp
var increment = (int value, int inc = 1) => value + inc;
Console.WriteLine(increment(10, 10));
Console.WriteLine(increment(10));
```

That outputs 20 and 11.

## Usage
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):
```csharp
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](https://sharplab.io/#v2:EYLgtghglgdgNAFxAJwK4wD4AEBMBGAWAChiA3CZAAlgGNkBTMemBSgXkoApZXyAbVPTjUWImu0p4AlOwB8lfoMoBqMQG5iWPAE5uMOo2YJOeAAzCzUqRqJbdtBkxYnTVtUA) where I set up the latest compiler version. Alternatively you can [download .NET 8 preview](https://github.com/dotnet/installer#installers-and-binaries). 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:
```xml
<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <LangVersion>preview</LangVersion>
    </PropertyGroup>

</Project>
```

## Resources
 * [The official proposal on GitHub](https://github.com/dotnet/csharplang/issues/6051)