Advertisement
Advanced C#, Modern C# Features (Global Usings, File-Scoped Namespaces, Top-Level Statements)

How do C# 12's default parameter values on LAMBDA expressions work, and what limitation did they remove compared to earlier C# versions?

Short answer

Prior to C# 12, lambda expressions couldn't declare DEFAULT parameter values the way regular methods and local functions could; C# 12 removes that restriction, letting a lambda assigned to a delegate type declare defaults directly, matching regular method flexibility.

Save C# mock interview
Advertisement
AModel answer
Prior to C# 12, lambda expressions couldn't declare DEFAULT parameter values the way regular methods and local functions could; C# 12 removes that restriction, letting a lambda assigned to a delegate type declare defaults directly, matching regular method flexibility.
var greet = (string name, string greeting = "Hello") => $"{greeting}, {name}!";
Console.WriteLine(greet("Sam")); // "Hello, Sam!" -- uses the default
Console.WriteLine(greet("Sam", "Hi")); // "Hi, Sam!"
Real-world example
Simplifying a small inline lambda-based utility that previously required a full local function just to support an optional parameter.
Likely follow-ups
  • Does this default-parameter lambda syntax work when the lambda is assigned to a built-in Func<> or Action<> delegate type?
Related topics