Lambda expressions with parameter modifiers (ref, out, in, params) required explicit type declarations, even when the type could be inferred from context. This made lambdas with modifiers more verbose than necessary.
C# 14.0 allows parameter modifiers to be used without explicit types when the type can be inferred, making modifier-based lambdas as concise as regular lambdas.
Code
C#
delegate bool TryParse(string s, out int value);
delegate int RefTransform(ref int value);
// Modifiers without explicit types
TryParse tryParse = (s, out value) => int.TryParse(s, out value);
RefTransform doubler = (ref x) => { x *= 2; return x; };
// scoped modifier
ReadOnlySpan<int> Process(ReadOnlySpan<int> data, Func<scoped ReadOnlySpan<int>, int> fn)
=> default;C#
delegate bool TryParse(string s, out int value);
delegate int RefTransform(ref int value);
// Modifiers required explicit types
TryParse tryParse = (string s, out int value) => int.TryParse(s, out value);
RefTransform doubler = (ref int x) => { x *= 2; return x; };Notes
- The compiler infers parameter types from the target delegate type, just like regular lambdas
- Works with
ref,out,in,scoped,ref readonly, andparamsmodifiers - Multi-parameter lambdas still require parentheses:
(ref x, out y) => ... - Cannot mix explicitly typed and implicitly typed parameters in the same lambda