This question came up in the comments of this answer. The inability to have readonly properties was proposed as a potential reason to use fields instead of properties.
For example:
class Rectangle { private readonly int _width; private readonly int _height; public Rectangle(int width, int height) { _width = width; _height = height; } public int Width { get { return _width; } } public int Height { get { return _height; } } }
But why can't you just do this?
public int Width { get; readonly set; }
Edit (clarification): You can achieve this functionality in the first example. But why can't you use the auto-implemented property shorthand to do the same thing? It would also be less messy, since you wouldn't have to directly access the fields in your constructor; all access would be through the property.
Edit (update): As of C# 6.0, readonly properties are supported! object MyProp { get; }
This property can be set inline (object MyProp { get; } = ...
) or in the constructor, but nowhere else (just like readonly
fields).
Create Readonly Property Read only means that we can access the value of a property but we can't assign a value to it. When a property does not have a set accessor then it is a read only property. For example in the person class we have a Gender property that has only a get accessor and doesn't have a set accessor.
A class property declared read-only is only allowed to be initialized once, and further changes to the property is not allowed. Read-only class properties are declared with the readonly keyword* in a typed property.
You can use mapping modifiers to change a readonly property to mutable in TypeScript, e.g. -readonly [Key in keyof Type]: Type[Key] . You can remove the readonly modifier by prefixing the readonly keyword with a minus - .
Because the language doesn't allow it.
This may seem like a frivolous answer: after all, the language designers could have declared that if you used readonly
on an automatic property then it would mean "the property is settable but only in the constructor".
But features don't come for free. (Eric Gunnerson expresses it as "Every feature starts with minus 100 points.") To implement read-only automatic properties would have required additional compiler effort to support the readonly modifier on a property (it currently applies only to fields), to generate the appropriate backing field and to transform sets
of the property to assignments to the backing field. That's quite a bit of work to support something that the user could do reasonably easily by declaring a readonly backing field and writing a one-line property getter, and that work would have a cost in terms of not implementing other features.
So, quite seriously, the answer is that either the language designers and implementers either never thought of the idea, or -- more likely -- they thought it would be nice to have, but decided there were better places to spend their finite resources. There's no technical constraint that prevents the language designers and implementers providing the feature you suggest: the reasons are more about the economics of software development.
If you want to make a property "read only" as far as functionality is concerned, you do so by only supplying the get
method, as you indicated in your post.
public int Width { get { return _width; } } public int Height { get { return _height; } }
The compiler will even reference these as "read only" if you try to write to them.
Having an additional term of readonly
for a property would clash with also providing the set
method. It seems to be poor syntax to me, i.e. how does the person reading it (or the compiler, for that matter) know what takes precedence: readonly
or set
?
Furthermore, as was explained in the answer you referenced, readonly
applies only to fields and limits writing to those fields to the instantiation of the class. With properties, you can't write to them (I don't think) even within the constructor if they only have a get
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With