-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Field-backed properties: readonly backing field #74922
Conversation
} | ||
// - The property has no set accessor (but may have an init accessor) and | ||
// the get accessor, if any, is automatically implemented. | ||
else if ((!hasSetAccessor || isInitOnly) && (!hasGetAccessor || hasAutoPropertyGet)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the field also be readonly if all manually implemented get
and set
accessors are declared readonly
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a slight leaning towards "no", but not a strong opinion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not valid to declare both accessors as readonly, you'd have to declare the containing property as readonly instead, which would make the field readonly per these rules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Declaring both as readonly
is an error, but we may want to treat the backing field as readonly
in the following case:
struct S
{
object P { get; readonly set { } }
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a more compelling example is:
struct S
{
object P { readonly get => field; init; }
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, I can't think of a case where things are made worse by the backing field not being readonly, in cases involving the accessors being marked readonly but not the containing type or property.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a PROTOTYPE comment and a test for now.
} | ||
// - The property has no set accessor (but may have an init accessor) and | ||
// the get accessor, if any, is automatically implemented. | ||
else if ((!hasSetAccessor || isInitOnly) && (!hasGetAccessor || hasAutoPropertyGet)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a slight leaning towards "no", but not a strong opinion.
<Q5>k__BackingField: True | ||
<Q6>k__BackingField: {{useReadOnlyType}} | ||
<Q7>k__BackingField: True | ||
<Q8>k__BackingField: {{useReadOnlyType}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels like in the case useReadOnlyType: false, useReadOnlyMember: true
, all accessors are readonly, yet the backing field is not readonly. That seems a little strange. I guess it doesn't really matter, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is an example of the case I was expecting in #74922 (comment).
From proposal: