3 ms·
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:
by xnorswap 1y ago
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.
- martijn_himself 1y ago> 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.
- xnorswap 1y agoThey 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/csharp-14#the-field-keyword https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/cs...
- martijn_himself 1y agoThis is great thank you.