[Proposal]: New expression for access backing field of auto-implemented property #4146
-
New expression for access backing field of auto-implemented property: fieldof()
SummaryMake programmer to access the backing field of auto-implemented property when necessary to be use it with ref param. MotivationWe cannot use property with ref param. If I want to use a property value in some place, I should to write a full property. Detailed design
DrawbacksMay will make readonly property be modifiable. AlternativesUnresolved questionsDesign meetings |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
At this point i would say that you shouldn't have an auto-prop.
This is particularly concerning, because it makes how a property works no longer an implementation detail. For example, you couldn't change your property from an auto-prop to a completely computed prop as that would break anyone that did The reason to use a property in the first plcae, and not just use a field, is entirely because the impl should be hidden and can be updated/replaced in the future without breaking anything that would change this. If you want ot be able to pass as a ref, and you want people to be able to use the field directly, then there is no value in usign a property. Just replace the property with a field. Now it will all work, and will have all the same limitations as you'd have if you used |
Beta Was this translation helpful? Give feedback.
-
Auto-properties are meant for code compactness and readability. Now, with C# 13, the new Moreover, the Having a IMHO, it should act exactly like the explicit backing field, that normally is private (or rarely protected, or maybe internal). About the readonly access, the compiler could enforce it based on what property accessors contain explicit code or depending on annotations on the property. |
Beta Was this translation helpful? Give feedback.
At this point i would say that you shouldn't have an auto-prop.
This is particularly concerning, because it makes how a property works no longer an implementation detail. For example, you couldn't change your property from an auto-prop to a completely computed prop as that would break anyone that did
fieldof
on it.The reason to use a property in the first plcae, and not just use a field, is entirely because the impl should be hidden and can be updated/replaced in the future without breaking anything that would change this.
If you want ot …