Skip to content

Commit c78ec07

Browse files
committed
Add additional diagnostics
A couple warnings have been added regarding field backed properties.
1 parent 8de8f55 commit c78ec07

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

docs/csharp/language-reference/compiler-messages/partial-declarations.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ f1_keywords:
4747
- "CS9257"
4848
- "CS9258"
4949
- "CS9263"
50+
- "CS9264"
51+
- "CS9266"
5052
helpviewer_keywords:
5153
- "CS0260"
5254
- "CS0261"
@@ -93,7 +95,8 @@ helpviewer_keywords:
9395
- "CS9257"
9496
- "CS9258"
9597
- "CS9263"
96-
ms.date: 11/01/2024
98+
- "CS9266"
99+
ms.date: 11/06/2024
97100
---
98101
# Errors and warnings related to `partial` type and `partial` member declarations
99102

@@ -148,6 +151,11 @@ That's by design. The text closely matches the text of the compiler error / warn
148151
- [**CS9258**](#field-backed-properties): *In this language version, the '`field`' keyword binds to a synthesized backing field for the property. To avoid generating a synthesized backing field, and to refer to the existing member, use '`this.field`' or '`@field`' instead.*
149152
- [**CS9263**](#field-backed-properties): *A partial property cannot have an initializer on both the definition and implementation.*
150153

154+
The following warnings can be generated for field backed properties:
155+
156+
- [**CS9264**](#field-backed-properties): *Non-nullable property must contain a non-null value when exiting constructor. Consider adding the 'required' modifier, or declaring the property as nullable, or adding '`[field: MaybeNull, AllowNull]`' attributes.**
157+
- [**CS9266**](#field-backed-properties): *One accessor of property should use '`field`' because the other accessor is using it.*
158+
151159
The following sections explain the cause and fixes for these errors and warnings.
152160

153161
## Partial types
@@ -245,9 +253,11 @@ public partial int ImplementingDeclaration { get => field; set; }
245253

246254
- **CS9258**: *In this language version, the '`field`' keyword binds to a synthesized backing field for the property. To avoid generating a synthesized backing field, and to refer to the existing member, use '`this.field`' or '`@field`' instead.*
247255
- **CS9263**: *A partial property cannot have an initializer on both the definition and implementation.*
256+
- **CS9264**: *Non-nullable property must contain a non-null value when exiting constructor. Consider adding the 'required' modifier, or declaring the property as nullable, or adding '`[field: MaybeNull, AllowNull]`' attributes.**
257+
- **CS9266**: *One accessor of property should use '`field`' because the other accessor is using it.*
248258

249259
[!INCLUDE[field-preview](../../includes/field-preview.md)]
250260

251261
Beginning with C# 13, the preview feature, `field` backed properties allows you to access the compiler synthesized backing field for a property. **CS9258** indicates that you have a variable named `field`, which can be hidden by the contextual keyword `field`.
252262

253-
**CS9263** indicates that your declaring declaration includes an implementation. That implementation might be accessing the compiler synthesized backing field for that property.
263+
**CS9263** indicates that your declaring declaration includes an implementation. That implementation might be accessing the compiler synthesized backing field for that property. **CS9264** indicates that the your use of `field` assumes a non-nullable backing field while the property declaration is nullable. The compiler assumes both the backing field and the property have the same nullability. You need to add the `[field:MaybeNull, AllowNull]` attribute to the property declaration to indicate that the `field` value should be considered nullable. **CS9266** indicates that one of a properties accessors uses the `field` keyword, but the other uses a hand-declared backing field. The warning indicates you may have done that by accident.

docs/csharp/language-reference/toc.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,8 @@ items:
482482
partial type, partial member, partial method, partial property, partial indexer,
483483
CS0260, CS0261, CS0262, CS0263, CS0264, CS0265, CS0267, CS0282, CS0501, CS0750, CS0751, CS0754, CS0755, CS0756, CS0757,
484484
CS0759, CS0761, CS0762, CS0763, CS0764, CS1067, CS8142, CS8663, CS8795, CS8796, CS8797, CS8798, CS8799, CS8800, CS8817,
485-
CS8818, CS8863, CS8988, CS9248, CS9249, CS9250, CS9251, CS9252, CS9253, CS9254, CS9255, CS9256, CS9257, CS9258, CS9263
485+
CS8818, CS8863, CS8988, CS9248, CS9249, CS9250, CS9251, CS9252, CS9253, CS9254, CS9255, CS9256, CS9257, CS9258, CS9263,
486+
CS9264, CS9266
486487
- name: Params modifier
487488
href: ./compiler-messages/params-arrays.md
488489
displayName: >

0 commit comments

Comments
 (0)