-
Notifications
You must be signed in to change notification settings - Fork 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
Open questions for field-backed properties #8231
Conversation
Aren't all auto properties "field backed"? That feels like a bad name for what is essentially a subset. I prefer semi-auto while realizing that there's a bias there; it's always been referred to as semi-auto, so I'm naturally more inclined to call it that. |
} | ||
``` | ||
|
||
In the example above, binding to the backing field should result in an error: "initializer cannot reference non-static field". |
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.
Is there an example where field binding to the backing property would have desirable behavior?
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 haven't found an example where binding to the backing field could be used in the property initializer.
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.
If so then why do we recommend making it a keyword? Seems like a breaking change that isn't balanced with a positive scenario.
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.
Also, if we did make field
a keyword in property initializer, I would expect to actually be able to read the field
in the initializer if the property is static. I feel like it gets wonky.
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.
Got a weirder one, which isn't on the list (and I don't know that I've seen it mentioned elsewhere). What about properties with expression body definitions?
public int X => Increment(ref field);
private int Increment(ref int field) => field++;
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.
Yeah those are property accessors and we expect field to be a keyword in them.
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 seems like the backing field of a static
property might be usable in the property initializer.
class MyClass
{
static object Property { get; } = Initialize(out field);
static object Initialize(out object obj)
{
return obj = new Random().Next();
}
}
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.
Yeah that's wonky and not useful IMO (you're already assigning the field, why do you need to be able to otherwise use it?). So put me in for wanting 'field' to just not be a keyword in the initializer, even though it might feel a little confusing when an accessor is using 'field' keyword in the same 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.
Got a weirder one, which isn't on the list (and I don't know that I've seen it mentioned elsewhere). What about properties with expression body definitions?
@HaloFour This was part of the intended design from the beginning, supporting lazy initialization such as that example and => field ??=
. #8069 fleshes out the proposal with examples like these.
You can definitely create a property which is not backed by a field: public int CurrentNumber => Random.Shared.Next(); |
@hez2010 that's not an auto property though, is it? I might be wrong, but I thought an auto property was properties where you don't reference any storage area (basically just |
@dotnet-policy-service rerun |
No description provided.