Skip to content
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

[Proposal]: fieldof() #9031

Open
RikkiGibson opened this issue Jan 8, 2025 · 0 comments
Open

[Proposal]: fieldof() #9031

RikkiGibson opened this issue Jan 8, 2025 · 0 comments
Assignees

Comments

@RikkiGibson
Copy link
Contributor

fieldof()

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);
            }
        }
    }
}

Design meetings

@dotnet dotnet locked and limited conversation to collaborators Jan 8, 2025
@RikkiGibson RikkiGibson self-assigned this Jan 9, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant