-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Open
Description
fieldof()
- Specification: https://github.com/dotnet/csharplang/blob/main/proposals/fieldof.md
- Discussion: Field keyword in properties: directly assign backing field in constructor #8704
- Related to [Proposal]:
field
keyword in properties #8635
Summary
Allow direct assignment and use of a property's backing field during construction, without having to invoke the setter, via a new fieldof(Prop)
expression.
class C
{
public C(DataStore store)
{
this.store = store;
// allows giving an initial value for 'this.Prop'
// without calling 'Store.WritePropToDisk()' thru the setter
fieldof(this.Prop) = store.ReadPropFromDisk();
// 'fieldof()' allows general usage of the field from an initialization context, a la writability of 'readonly' fields.
M(ref fieldof(this.Prop));
}
void Method()
{
// error: 'fieldof' can only be used during initialization
fieldof(this.Prop) = "a";
}
private DataStore store;
public string Prop
{
get => field;
set
{
if (value != field)
{
field = value;
store.WritePropToDisk(value);
}
}
}
}