skip to content

What did C# 12 and C# 14 change about C# lambda parameters, such as default values, params and writing out without a type?

level: middleimportance: nice to knowfreq 25%

answer

  1. two releases, two parameter features
  2. defaults need explicit types
  3. modifier without a type
  4. params still needs a type

basics

~20 s

C# 12 lets explicitly typed lambda parameters have default values and params arrays. C# 14 lets implicitly typed parameters carry ref, out, in, ref readonly or scoped without writing the type; params still needs a type, and defaults still need explicit types.

solid answer

~40 s

Since **C# 12**, an explicitly typed lambda may declare default values, `(string name = "guest") => ...`, and a final `params` parameter. Such a lambda has no matching `Func` or `Action`, so `var` gives it a compiler-synthesized delegate type that keeps the default, and `label()` works. Assigning it to `Func<string, string>` compiles with warning CS9099 and the default is lost: calling without the argument fails with CS7036. Implicitly typed parameters cannot have defaults (CS9098). **C# 14** removed the rule that a parameter with a modifier needed its type: `(text, out result) => int.TryParse(text, out result)` now converts to a delegate with an `out` parameter, and `ref`, `in`, `ref readonly` and `scoped` work the same way. The modifier is still required where the delegate has one (CS1676), and `params` still needs explicit types.

code

csharp · 18 lines
csharp
var label = (string name = "guest") => $"Hello, {name}";
Console.WriteLine(label());
Console.WriteLine(label("Ada"));
Console.WriteLine(label.GetType().Name);

var sum = (params int[] values) => values.Sum();
Console.WriteLine(sum(1, 2, 3));

TryParse<int> parse = (text, out result) => int.TryParse(text, out result);
Console.WriteLine(parse("42", out var n) ? n : -1);

Scale scale = (ref value, factor) => value *= factor;
int price = 10;
scale(ref price, 3);
Console.WriteLine(price);

delegate bool TryParse<T>(string text, out T result);
delegate void Scale(ref int value, int factor);

go deeper

for a junior

Recall that since C# 12 a lambda can have a default parameter value, written with explicit parameter types.

for a middle

Explain why a lambda with a default gets a synthesized delegate type, and what C# 14 allows for out and ref parameters without types.

for a senior

Know which language version each form needs and the diagnostics older compilers give, so shared code does not break a project on an older LangVersion.

for a principal

Decide when a team should declare custom delegate types to carry defaults rather than relying on synthesized types that callers cannot name.

## Two releases, two gaps closed Lambda parameters used to be a reduced version of method parameters. Two recent releases closed most of the gap: | Release | What lambdas gained | Restriction that remains | |---|---|---| | **C# 12** (.NET 8) | default parameter values; a final `params` array or collection | only in an **explicitly typed** parameter list | | **C# 14** (.NET 10) | `ref`, `out`, `in`, `ref readonly`, `scoped` on **implicitly typed** parameters | `params` still needs explicit types; a lambda with no parentheses cannot use a modifier | Older language versions reject the newer syntax with a feature diagnostic: under C# 11 a default value reports **CS9058** ("Feature 'lambda optional parameters' is not available in C# 11.0"), and under C# 13 an untyped `out` parameter reports **CS9260** ("Feature 'simple lambda parameter modifiers' is not available in C# 13.0"). ## Default values and params (C# 12) ```csharp var label = (string name = "guest") => $"Hello, {name}"; label(); // "Hello, guest" label("Ada"); // "Hello, Ada" ``` The syntax and rules for the default value are the ones methods use, so the value must be a compile-time constant and optional parameters come after required ones (**CS1737** otherwise). Three details matter: - **Explicit types only.** `(x = 3) => ...` reports **CS9098**, "Implicitly typed lambda parameter 'x' cannot have a default value." - **No `Func` or `Action` carries a default.** With `var`, the compiler synthesizes an anonymous delegate type (its name begins `<>f__AnonymousDelegate` on the .NET 10 SDK) that includes the default; how the compiler chooses a lambda's natural type is covered with delegate types. - **Converting to a delegate without the default drops it.** `Func<string, string> f = (string name = "guest") => name;` compiles with warning **CS9099**, and `f()` then fails with **CS7036**, because `Func<string, string>` has a required parameter. Declare your own delegate type with the default if callers need it. A final `params` parameter works the same way, `var sum = (params int[] values) => values.Sum();`, so `sum(1, 2, 3)` returns 6. ## Modifiers on simple parameters (C# 14) Before C# 14, any modifier forced you to type every parameter: ```csharp delegate bool TryParse<T>(string text, out T result); TryParse<int> parse2 = (string text, out int result) => int.TryParse(text, out result); ``` C# 14 lets the types be inferred from the delegate while keeping the modifier: ```csharp TryParse<int> parse1 = (text, out result) => int.TryParse(text, out result); ``` The rules from the C# 14 proposal: 1. Allowed modifiers are `ref`, `out`, `in`, `ref readonly`, `scoped`, `scoped ref` and `scoped ref readonly`. 2. A modifier is still **required** where the delegate's parameter has one: `(text, result) => ...` against `TryParse<int>` reports **CS1676**, "Parameter 2 must be declared with the 'out' keyword." 3. The list still cannot **mix** typed and untyped parameters: `(string text, out result)` reports **CS0748**. 4. It needs **parentheses**: `ref x => ...` is not legal. 5. `params` still requires an explicitly typed list. What each modifier means for the caller (by-reference passing, `scoped` lifetimes) is unchanged and is covered with method parameters and ref structs. ## Attributes on lambdas Since C# 10, a lambda and its parameters may carry attributes, provided the parameter list is in parentheses: `[return: NotNullIfNotNull(nameof(s))] (int? s) => ...`. Microsoft's documentation notes that they have **no effect when the delegate is invoked**; they serve analyzers and reflection, and `[Conditional]` cannot be applied to a lambda. ## Adopting the newer forms These are compiler features: they need the language version, not a newer runtime API. A project targeting .NET 10 gets C# 14 by default; a library that multi-targets older frameworks with an older `LangVersion` must keep the explicitly typed forms. Practical guidance: - prefer the C# 14 form for `out` and `ref` callbacks once every project that compiles the file is on C# 14; - when a delegate needs a default value that callers rely on, declare a named delegate type instead of relying on `var`, since the synthesized type cannot be named in a field, parameter or return type; - treat warning **CS9099** as a bug: the default you wrote is silently unreachable through that delegate. ## How interviewers probe it - Write a `TryParse`-style delegate and ask for the shortest lambda that satisfies it in C# 14. - Ask why a lambda with a default value cannot be stored in a `Func` without losing the default. - Ask which release a team needs before using untyped `out` parameters, and what an older compiler reports.

  • In C#, how do you keep a lambda's default parameter value when you need to store it in a field rather than in var?
    Declare a delegate type whose parameter has the same default, such as `delegate string Greeter(string name = "guest");`, and type the field with it. `Func` and `Action` cannot express a default, so converting to them drops it with warning CS9099.
  • In C# 14, can a lambda with an untyped scoped ref parameter be written without parentheses?
    No. Modifiers on untyped parameters apply only to a parenthesized parameter list, so `(scoped ref entry) => ...` is legal and `ref entry => ...` is not. The parenthesized form is also required whenever a lambda or its parameters carry attributes.

  • Says lambdas still cannot have default parameter values in current C#.
  • Believes Func<string, string> keeps the lambda's default value.
  • Thinks C# 14 lets you drop the out keyword when the type is inferred.
  • Claims params works on untyped lambda parameters since C# 14.
  • Puts a default value on an implicitly typed lambda parameter.

Assumes C# 14 on .NET 10; lambda default values and params need C# 12, modifiers on untyped lambda parameters need C# 14 (CS9260 in C# 13).