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: Keyword property access to backing field #1470

Closed
jademinear opened this issue Apr 21, 2018 · 2 comments
Closed

Proposal: Keyword property access to backing field #1470

jademinear opened this issue Apr 21, 2018 · 2 comments

Comments

@jademinear
Copy link

Problem

Consider the following auto-property:

public int MyProperty
{
    get;
    set;
}

MyProperty.get() returns a hidden backing store. Modifying the get() and set() methods of a property is a common practice. However, doing so requires that we declare a new backing store:

private int myVar;

public int MyProperty
{
    get { return myVar; }
    set { myVar = value; }
}

Rarely, if ever, will the backing store be directly accessed through code in a way that adds any additional functionality when the access modifier is private. How about exposing the hidden field of a property with the keyword field , to avoid writing unnecessary code when modifying the get() and set() of a property?

public int MyProperty
{
    get { return field; }
    set { field = value; }
}

Modified properties are declared often enough that this should warrant some quicker and cleaner alternatives, right?

@Joe4evr
Copy link
Contributor

Joe4evr commented Apr 21, 2018

Duplicate of #140

@jnm2
Copy link
Contributor

jnm2 commented Apr 4, 2020

Using a backing field without naming it was considered in https://github.com/dotnet/csharplang/blob/master/meetings/2020/LDM-2020-04-01.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants