←back to thread

.NET 10

(devblogs.microsoft.com)
489 points runesoerensen | 1 comments | | HN request time: 0.199s | source
Show context
martijn_himself ◴[] No.45899123[source]
I usually feel ambivalence with announcements of new C# versions.

Yes, a lot of great features have been added over the years. But it also introduces some amount of cognitive load and confusion. Take the first new feature in the example:

> Field-backed properties simplify property declarations by eliminating the need for explicit backing fields. The compiler generates the backing field automatically, making your code cleaner and more maintainable.

Huh, I thought we have had this for years, what were they called, ah Auto-Implemented Properties- so why is there a need for this? In this example:

  // Automatic backing field with custom logic
  public string Name
  {
      get => field;
      set => field = value?.Trim() ?? string.Empty;
  }
Ah so it's a way to access the backing field of Auto-Implemented Properties if you need more logic. And in the above can we just say:

  get;
or do you need to refer to the field keyword explicitly in the getter if we use it in the setter?

I feel like the documentation is always somewhat lacking in explaining the reasoning behind new features and how it evolves the language from earlier versions.

replies(3): >>45901384 #>>45902293 #>>45906217 #
1. wvenable ◴[] No.45906217[source]
I love this feature. I have avoided doing some stuff in properties because I don't want the noise of a private backing field.