Auto-property syntax: {get; private readonly var;} and related forms

Topics: C# Language Design
May 15, 2014 at 8:10 PM
In discussions of read-only auto-properties, a common question that arises is how to indicate that the value should be writable only in the constructor. Another related auto-property issue is how to allow structures to initialize auto-properties in their constructor without having to first explicitly zero out all fields.

I would suggest that both of these issues could be resolved by extending auto-property syntax so that code could access the backing field.

A property declaration of the form
ACCESS PROP_TYPE PROP_NAME { get; private READONLY var;}
would be equivalent to
private READONLY PROP_TYPE UNPRONOUNCEABLE_NAME;
ACCESS PROP_TYPE PROP_NAME NAME { get {return UNPRONOUNCEABLE_NAME;} }
but all references to PROP_NAME within the class would substitute UNPRONOUNCEABLE_NAME.

A property definition of the form
ACCESS PROP_TYPE PROP_NAME NAME { get; VAR_ACCESS READONLY var VAR_NAME;}
would be equivalent to
VAR_ACCESS READONLY PROP_TYPE VAR_NAME;
ACCESS PROPTYPE PROP_NAME NAME { get {return VAR_NAME;} }
A property definition of the form
ACCESS PROP_TYPE PROP_NAME NAME { get; SET_ACCESS set; VAR_ACCESS READONLY var VAR_NAME;}
would be equivalent to
VAR_ACCESS READONLY PROP_TYPE VAR_NAME;
ACCESS PROPTYPE PROP_NAME NAME {
  get { return VAR_NAME; } 
  SET_ACCESS set { VAR_NAME = value; }
}
I would expect that the first formation would be the most common, but the later ones would be helpful in cases where the sole reason for using a property is allow outside code read-only access to the variable, and where the base class could not change the behavior of the auto-property without breaking derived class code.
Developer
May 15, 2014 at 8:43 PM
supercat wrote:
In discussions of read-only auto-properties, a common question that arises is how to indicate that the value should be writable only in the constructor.
I haven't seen anything about that. The current syntax (get-only autoprops are readonly) seems clean and simple. Adding syntax doesn't seem necessary.
Another related auto-property issue is how to allow structures to initialize auto-properties in their constructor without having to first explicitly zero out all fields.
Are you referring to bug 128? I think I just need to fix definite assignment analysis here.
May 15, 2014 at 9:38 PM
angocke wrote:
I haven't seen anything about that. The current syntax (get-only autoprops are readonly) seems clean and simple. Adding syntax doesn't seem necessary.
There were questions about how the value of a get-only auto-property should be set within a class constructor; if it had been definitely resolved that auto-properties with no defined setter should be accessible within constructors but not elsewhere, I was unaware of such resolution.
Another related auto-property issue is how to allow structures to initialize auto-properties in their constructor without having to first explicitly zero out all fields.
Are you referring to bug 128? I think I just need to fix definite assignment analysis here.
I don't know of a related bug number, but it's an issue that has appeared a few times on SO. If writing to a property is a method invocation which passes this as a ref parameter, the rules of the language would require that all fields of this must be assigned beforehand. If property writes are simply replaced by field writes, that would not be required.

Many property definitions (I would guess the vast majority of non-auto properties) have a getter whose sole purpose is to return the value of a backing field. Even in cases where a set would be have to do something besides write the backing field, I would think that declaring a property as having a particular backing field and default get method [that simply returns it] would be cleaner than having to declare a backing field and declare a get which explicitly returns it.