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.
Ben McCormack Jan 30 '10 at 3:52 2010-01-30 03:52
source share