-
Type of issueOther (describe below) DescriptionReading the Docs I am missing one sample for a relatively common usecase I have had multiple times in Apps so far. We would have for example this (before C# 14) Definition in our code: public abstract record BaseNotificationAggregator
{
public int Id { get; set; }
private readonly List<BaseNotification> _Notifications = new();
public IReadOnlyCollection<BaseNotification> Notifications => _Notifications.AsReadOnly();
public void AddNotification(BaseNotification baseNotification)
{
_Notifications.Add(baseNotification);
}
public void RemoveNotification(BaseNotification baseNotification)
{
_Notifications.Remove(baseNotification);
}
public void ClearNotification()
{
_Notifications.Clear();
}
}where you can find our BaseNotificationAggregator
I would not really see a way (except from we suddenly can use >2 Accessors in a Property?) to tell the compiler that, while I want to show the Property as Page URLContent source URLhttps://github.com/dotnet/csharplang/blob/main/proposals/csharp-14.0/field-keyword.md |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
|
I do not see a way to use |
Beta Was this translation helpful? Give feedback.
-
|
You can't have the backing field be a different type from the autoproperty, so using |
Beta Was this translation helpful? Give feedback.
-
|
One of the central tenets of the field keyword feature is protection of the backing field from access by the class. You use |
Beta Was this translation helpful? Give feedback.
One of the central tenets of the field keyword feature is protection of the backing field from access by the class. You use
fieldwhen the class should have no special access to the backing storage and must only interact through the property directly. However, in your use case, you want the class to have direct access to the backing storage. The vehicle for that scenario is to declare a field so that the class has something to access directly.