-
Notifications
You must be signed in to change notification settings - Fork 4.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
Automatic property backing field #16926
Comments
With that, it could even be further simplified by allowing default implementation for the accessor you don't explicitly define: public int MyInt
{
get; // Implicit "return var"
set
{
if (value < MinValue) var = MinValue;
else if (value > MaxValue) var = MaxValue;
else var = value;
}
} |
Unfortunately this will break existing code where a field named |
This is true. Perhaps using the keyword base could be a valid alternative? I believe base is not a valid variable name since the first C# version. |
This appears to be a C# language design proposal. Language design proposals should be opened for discussion on the csharplang mailing list, and if sufficient support is given, it should be submitted as a pull request to add a proposal in markdown at the csharplang repository. |
I didn't know about the proper procedure. I'll do that now. |
Using a backing field without naming it was considered in https://github.com/dotnet/csharplang/blob/master/meetings/2020/LDM-2020-04-01.md |
Properties are often used to do simple checks on the values that are being assigned to a member, but this requires the declaration of a backing field like so:
This has a couple of disadvantages:
I think the language should support automatic backing fields, which should only be accessible from the property's get and set declarations.
The backing field could reuse the 'var' keyword like so:
The text was updated successfully, but these errors were encountered: