Language: C# | Type: CODE_SMELL | Severity: Critical
Tags: csharp-14, compatibility, upgrade
This rule raises an issue when code uses 'field' as an identifier in contexts where it conflicts with the new field contextual keyword
introduced in C# 14.
C# 14 introduces the field contextual keyword for field-backed properties. This keyword allows access to the synthesized backing
fields directly within property accessors, simplifying property implementations that need custom logic.
By using 'field' as an identifier, several problems can occur:
field within property accessors cause compiler
error CS9273. References to existing members named field within property accessors cause compiler error CS9258. See Compiler errors on property
declarations.field may be unexpectedly overshadowed by the synthesized backing
field when referenced within property accessors, leading to logic errors and unexpected runtime behavior.Using 'field' as an identifier can prevent successful compilation when upgrading to C# 14. In property accessors, local variables or parameters named 'field' will cause compilation errors, while class members named 'field' may be unexpectedly overshadowed by synthesized backing fields, leading to logic errors and unexpected runtime behavior.
Rename the identifier, escape it by prefixing with @, or qualify member access with this. or base. to avoid
conflicts with the contextual keyword. This rule applies only inside property get, set, and init accessors;
indexer and event accessors are not affected.
public class ClassFieldExample
{
private string field;
public string Message
{
get => field; // Noncompliant
set => field = value; // Noncompliant
}
}
public class LocalFunctionExample
{
public int Value
{
get
{
return LocalFunction(42);
int LocalFunction(int field) // Noncompliant
=> field;
}
}
}
public class ClassFieldExample
{
private string field;
public string Message
{
get => this.field; // or @field
set => this.field = value; // or @field = value;
}
}
public class LocalFunctionExample
{
public int Value
{
get
{
return LocalFunction(42);
int LocalFunction(int value) // Compliant - renamed
=> value;
}
}
}
field
keywordfield in a property accessor refers to synthesized backing fieldfield disallowed in a property accessor (documentation incorrectly references CS9272; the actual error code is CS9273)