Language: C# | Type: CODE_SMELL | Severity: Critical
Tags: csharp14, breaking-change, lambda
Using scoped as a type name in lambda parameter lists will cause a compilation error when upgrading to C# 14 or later.
In C# 14, the language introduced the ability to write lambdas with parameter modifiers without specifying parameter types. As part of this
enhancement, a breaking change was implemented where scoped is always treated as a modifier in lambda parameter lists.
As a result, parenthesized lambda parameters that use scoped as an identifier or type name will fail to compile after upgrading to C#
14. In that context, the compiler interprets scoped as a modifier unless it is escaped with @.
This breaking change affects existing code that was valid in earlier C# versions, particularly when upgrading projects to C# 14. The issue is
specific to lambda expressions and doesn’t affect other contexts where the scoped type name might be used.
This issue prevents code compilation when upgrading to C# 14. Projects that previously compiled successfully will fail to build, blocking development and deployment processes until the code is updated.
Rename the type, or escape the type name scoped with the @ prefix. This tells the compiler to treat it as an identifier
rather than a contextual keyword.
Action<int> lambda1 = (scoped) => { }; // Noncompliant
var lambda2 = (scoped scoped) => { }; // Noncompliant
var lambda3 = (scoped scoped parameter) => { }; // Noncompliant
ref struct @scoped { }
Action<int> lambda1 = (@scoped) => { };
var lambda2 = (@scoped scoped) => { };
var lambda3 = (scoped @scoped parameter) => { };
ref struct @scoped { }
var lambda = (scoped scoped parameter) => { }; // Noncompliant
ref struct @scoped { }
var lambda = (scoped ScopedItem parameter) => { };
ref struct ScopedItem { }