Optional arguments C# 4.0 allow parameters to a method to assume a default in the absence of an explicit passed value.
C# 12.0 extends this to lambda expressions C# 3.0.
Code
csharp
var increment = (int x, int y = 1) => x + y;csharp
var increment = (int x, int y) => x + y;
var incrementBy1 = (int x) => increment(x, 1);Notes
- The default value is part of the lambda's inferred delegate type, so it only applies when called through
var(or a delegate type declaring the same default); assigning the lambda toFunc<int, int, int>drops it and produces warning CS9099 - C# 12 also allows
paramsarrays on lambda parameters, such as(params int[] values) => values.Sum()