← back to index

S8368 — Identifiers should not conflict with the C# 14 "extension" contextual keyword

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.

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:

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