←back to thread

.NET 10

(devblogs.microsoft.com)
489 points runesoerensen | 7 comments | | HN request time: 0s | source | bottom
1. 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 #
2. 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 #
3. 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 #
4. xnorswap ◴[] No.45901740{3}[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 #
5. martijn_himself ◴[] No.45901833{4}[source]
This is great thank you.
6. anonymars ◴[] No.45902293[source]
Agreed. I feel like we're getting diminishing returns out of the language as they try to squeeze out every last keystroke (as though the challenge of software development is in the typing)

I'm thankful I've been along for the ride so I know the "archaeology" but pity those freshly dunked into its increasingly complicated ocean

7. 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.