From 9793750ee2080b34bf727d61c5a4ce72e7ce4874 Mon Sep 17 00:00:00 2001 From: Charles Stoner <10732005+cston@users.noreply.github.com> Date: Thu, 20 Jun 2024 10:58:37 -0700 Subject: [PATCH 1/4] Open questions for field-backed properties --- proposals/semi-auto-properties.md | 60 +++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/proposals/semi-auto-properties.md b/proposals/semi-auto-properties.md index a09983c408..2595db79df 100644 --- a/proposals/semi-auto-properties.md +++ b/proposals/semi-auto-properties.md @@ -156,6 +156,66 @@ public class Point - https://github.com/dotnet/csharplang/issues/5563 - https://github.com/dotnet/csharplang/pull/5573#issuecomment-1002110830 +### Feature name + +Some options for the name of the feature: +1. semi-auto properties +1. field-backed properties +1. field keyword + +### `field` in property initializer + +Should `field` be a keyword in a property initializer and bind to the backing field? + +```csharp +class MyClass +{ + private const int field = -1; + + public object Property { get; } = field; // bind to const (ok) or backing field (error)? +} +``` + +In the example above, binding to the backing field should result in an error: "initializer cannot reference non-static field". + +**Recommendation**: `field` is a keyword within a property initializer, and binds to the backing field. + +### `value` in event accessor + +Should `value` be a keyword in an event accessor? + +```csharp +class MyClass +{ + private EventHandler _e; + + public event EventHandler E + { + add { _e += value; } + remove { _e -= value; } + } +} +``` + +**Recommendation**: `value` is a keyword within an event accessor. + +### `field` in event accessor + +Should `field` be a keyword in an event accessor with the compiler generating a backing field? + +```csharp +class MyClass +{ + public event EventHandler E + { + add { field += value; } + remove { field -= value; } + } +} +``` + +**Recommendation**: `field` is *not* a keyword within an event accessor, and no backing field is generated. + ## LDM history: - https://github.com/dotnet/csharplang/blob/main/meetings/2021/LDM-2021-03-10.md#field-keyword - https://github.com/dotnet/csharplang/blob/main/meetings/2021/LDM-2021-04-14.md#field-keyword From 606c1feb89586400311d2e3757a7cac41c6b8f12 Mon Sep 17 00:00:00 2001 From: Charles Stoner <10732005+cston@users.noreply.github.com> Date: Thu, 20 Jun 2024 11:29:20 -0700 Subject: [PATCH 2/4] Group event accessor questions --- proposals/semi-auto-properties.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/proposals/semi-auto-properties.md b/proposals/semi-auto-properties.md index 2595db79df..17983a2b1e 100644 --- a/proposals/semi-auto-properties.md +++ b/proposals/semi-auto-properties.md @@ -180,7 +180,7 @@ In the example above, binding to the backing field should result in an error: "i **Recommendation**: `field` is a keyword within a property initializer, and binds to the backing field. -### `value` in event accessor +### `field` and `value` in event accessor Should `value` be a keyword in an event accessor? @@ -199,9 +199,7 @@ class MyClass **Recommendation**: `value` is a keyword within an event accessor. -### `field` in event accessor - -Should `field` be a keyword in an event accessor with the compiler generating a backing field? +Should `field` be a keyword in an event accessor, and should the compiler generate a backing field? ```csharp class MyClass From 37ec84277fb4688de923773a29009ee70934d922 Mon Sep 17 00:00:00 2001 From: Charles Stoner <10732005+cston@users.noreply.github.com> Date: Thu, 20 Jun 2024 16:25:59 -0700 Subject: [PATCH 3/4] Remove recommendation --- proposals/semi-auto-properties.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/proposals/semi-auto-properties.md b/proposals/semi-auto-properties.md index 17983a2b1e..a18f1d41f1 100644 --- a/proposals/semi-auto-properties.md +++ b/proposals/semi-auto-properties.md @@ -178,8 +178,6 @@ class MyClass In the example above, binding to the backing field should result in an error: "initializer cannot reference non-static field". -**Recommendation**: `field` is a keyword within a property initializer, and binds to the backing field. - ### `field` and `value` in event accessor Should `value` be a keyword in an event accessor? From 99fc5d27a8bf44ff18ae6c5f66c6b1fa39d44d38 Mon Sep 17 00:00:00 2001 From: Charles Stoner <10732005+cston@users.noreply.github.com> Date: Fri, 21 Jun 2024 10:30:13 -0700 Subject: [PATCH 4/4] Add name option --- proposals/semi-auto-properties.md | 1 + 1 file changed, 1 insertion(+) diff --git a/proposals/semi-auto-properties.md b/proposals/semi-auto-properties.md index a18f1d41f1..c326e92aba 100644 --- a/proposals/semi-auto-properties.md +++ b/proposals/semi-auto-properties.md @@ -160,6 +160,7 @@ public class Point Some options for the name of the feature: 1. semi-auto properties +1. field access for auto properties [LDM-2023-07-17](https://github.com/dotnet/csharplang/blob/main/meetings/2023/LDM-2023-07-17.md#compiler-check-in) 1. field-backed properties 1. field keyword