←back to thread

.NET 10

(devblogs.microsoft.com)
489 points runesoerensen | 2 comments | | HN request time: 0.415s | 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 #
xnorswap ◴[] No.45901384[source]
Previously, if you wanted to add that Trim in, you needed to define the field behind.

You could have:

    public string Name { get;set; }
Or you could have:

    private string name;

    public string Name {
      get;
      set { this.name = value?.Trim() ?? string.Empty; }
    }
So you needed in the second case to also declare name as a field. The new syntax avoids having to do that "double" declaration.
replies(1): >>45901457 #
martijn_himself ◴[] No.45901457[source]
> The new syntax avoids having to do that "double" declaration.

Yes, that's right. It is in other words a way to access the compile-time generated backing field for auto-implemented properties. It is quite nice to be honest, I just wish they presented a bit of context in their announcements.

replies(1): >>45901740 #
1. xnorswap ◴[] No.45901740[source]
They provide a bit more context around changes in their "What's new in C# 14" page:

https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/cs...

replies(1): >>45901833 #
2. martijn_himself ◴[] No.45901833[source]
This is great thank you.