Why properties cannot be read only?

This question arose in the comments of this answer . The inability to have readonly properties has been suggested 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 it?

 public int Width { get; readonly set; } 

Change (clarification) . You can execute this function in the first example. But why can't you use automatically implemented property reduction to do the same? This would also be less messy, since you didn't have to directly access the fields in your constructor; all access will be through the property.

+31
c # properties
Jan 30 '10 at 3:48
source share
6 answers

Because the language does not allow this.

This may seem like a frivolous answer: language developers could declare that if you used readonly in an automatic property, it would mean that the property can be set, but only in the constructor.

But features are not provided for free. (Eric Gannners expresses this as β€œEach function starts with minus 100 points .) To implement automatic read-only properties, it would take an extra compiler effort to maintain the readonly modifier on the property (it currently applies to fields only) to create the corresponding support field and conversion sets the properties in the field assignment support. It's quite a lot of work to support something that the user could do quite easily by declaring the field is read-only for reading and writing odes ostrochny getter, and this work will cost in terms of not implementing other functions.

So, quite seriously, the answer is that either the language developers or the developers never thought about this idea, or rather, they thought it would be nice to have, but decided that there are better places to hold the final resources. There are no technical restrictions that impede the developers and language developers providing the feature you are offering: the reasons are more about the economics of software development.

+21
Jan 30 '10 at 4:00
source share

If you want to make the property read-only with respect to functionality, you only do this by supplying the get method, as you indicated in your post.

 public int Width { get { return _width; } } public int Height { get { return _height; } } 

The compiler even refers to them as read-only if you try to write to them.

Having an additional readonly member for a property would be confronted with the provision of the set method. It seems to me that this is bad syntax, i.e. How does the person reading it (or the compiler, for that matter) know that it has an advantage: readonly or set ?

Also, as explained in the answer you refer to, readonly only applies to fields and limits that write these fields to an instance of the class. With properties, you cannot write to them (I don’t think) even inside the constructor if they only have a get method.

+15
Jan 30 '10 at 3:52
source share

You can make a read-only automatic property by specifying a private access modifier for this type

 public bool Property {get; private set;} 

The installer is still defined, but it is no longer visible outside the class where the property is defined. On the sidelines, it is sometimes useful to define the setter as internal so that the properties can be easily set from one assembly, but not with the help of external callers.

+9
Jan 30 '10 at 4:00
source share

Properties may be read-only and not automatic.

Automatic properties require both get and set , and it makes no sense for a read-only property to have set .

You can define a regular property as a read-only property by simply defining get - however, even if the requirement for get and set for automatic properties did not exist - the read-only property cannot be automatically determined because you must know the support field to have the ability to install it inside (i.e. through the constructor).

I assume that in VS there may be a template / macro or something specific in VS to generate the code, but it could not be part of the language itself.

+2
Jan 30 '10 at 3:55
source share

I think the main problem is that the properties are just the syntactic sugar for the field with the optional getter / setter methods. Automatic properties generate a support field so that they require a "setter", or there would be no way to set the value of the support field. Since properties are really mapped to methods, not fields, it makes no sense to readonly them.

Even if enabled, readonly can only be applied to automatic properties. For traditional properties, you can put arbitrary code in both getter and setter. Even if the installer could only be called in the constructor of the class, the getter could still change the value based on any logic that you decided to insert into it. This would completely contradict the concept of readonly , which would require different syntax rules and support for automatic / traditional properties. Since there is a mechanism - using traditional properties using only a specific getter AND read-only read-only fields, as in the reference question - I see no reason to reset the syntax of the properties and potentially create confusion for something with a fairly simple and simple implementation using existing language constructs.

+1
Jan 30 '10 at 4:07
source share

If the property has a private set, then it is found only from the outside world, that is:

 string _name; public string Name { get{ return _name; } private set { _name = value; } } 

Or, this can be done read-only if it does not have a setter at all, that is:

 string _name; public string Name { get{ return _name; } } 
0
Jan 30 '10 at 11:33
source share



All Articles