Identifiers named extension should be renamed or use the @ escape prefix to avoid breaking changes in C# 14 or later.
Why is this an issue?
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:
- Type declaration names
- Type parameter names
- Constructor names
- Using alias names
- Return types, property types, and field types (including arrays and pointers)
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.
What is the potential impact?
Code that currently compiles will fail to compile when the project is upgraded to C# 14.
How to fix it
Rename the identifier, or add the @ prefix to it.
Code examples
Noncompliant code example
class extension { }
class MyClass
{
extension field;
extension Property { get; }
}
Compliant solution
Escape the identifier with the @ verbatim prefix:
class @extension { }
class MyClass
{
@extension field;
@extension Property { get; }
}
Noncompliant code example
class extension { }
class MyClass
{
extension ReturnType() { return default; }
}
Compliant solution
Rename the identifier:
class MyExtension { }
class MyClass
{
MyExtension ReturnType() { return default; }
}
Resources
Documentation
- Microsoft Learn -
extensiontreated as a contextual keyword - Microsoft Learn - Extension member declarations
- Microsoft Learn - Explore extension members in C# 14
- Microsoft Learn - Verbatim identifiers
(
@prefix)