Roslyn: Automatic property backing field

Created on 3 Feb 2017  路  7Comments  路  Source: dotnet/roslyn

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:

/// <summary>
/// Backing field for 'MyInt' property.
/// </summary>
private int _MyInt;

/// <summary>
/// Actual MyInt property.
/// </summary>
public int MyInt
{
    get { return _MyInt; }
    set
    {
        if (value < MinValue) _MyInt = MinValue;
        else if (value > MaxValue) _MyInt = MaxValue;
        else _MyInt = value;
    }
}

This has a couple of disadvantages:

  1. Having to declare the backing field is, in my opinion, verbose.
  2. The backing field can still be accessed and modified directly, bypassing completely the property (although in some cases this behavior may be acceptable).

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:

/// <summary>
/// MyInt property with automatic backing field.
/// </summary>
public int MyInt
{
    get { return var; }
    set
    {
        if (value < MinValue) var = MinValue;
        else if (value > MaxValue) var = MaxValue;
        else var = value;
    }
}
Area-Language Design Resolution-External

Most helpful comment

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:

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

All 7 comments

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:

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

Check out #850 and #8364.

Unfortunately this will break existing code where a field named var exists. (With the new compiler, the new backing field will be set/read, instead of the existing field).

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

Was this page helpful?
0 / 5 - 0 ratings