Language: C# | Type: CODE_SMELL | Severity: Critical
Tags: csharp14, breaking-change, contextual-keyword
Identifiers named extension should be renamed or use the @ escape prefix to avoid breaking changes in C# 14 or later.
Starting with C# 14, extension is
a contextual keyword for declaring extension members and extension containers. This change can cause compilation errors in existing code that uses
the word extension as an identifier. For example, a class named 'extension' will be interpreted as an extension container declaration,
leading to parsing failures.
This rule flags any unescaped extension identifiers used in the following cases:
Note that method names, parameter names, and local variable names are not affected by this breaking change. For example, void extension() {
} remains valid in C# 14.
Code that currently compiles will fail to compile when the project is upgraded to C# 14.
Rename the identifier, or add the @ prefix to it.
class extension { }
class MyClass
{
extension field;
extension Property { get; }
}
Escape the identifier with the @ verbatim prefix:
class @extension { }
class MyClass
{
@extension field;
@extension Property { get; }
}
class extension { }
class MyClass
{
extension ReturnType() { return default; }
}
Rename the identifier:
class MyExtension { }
class MyClass
{
MyExtension ReturnType() { return default; }
}
extension treated as a contextual keyword@ prefix)