Note that there are some explanatory texts on larger screens.

plurals
  1. POWrite-Only properties, what's the point?
    primarykey
    data
    text
    <p>I understand why you would want to use a read-only property using the following syntax:</p> <pre><code>private int _MyInt; public int MyInt { get { return _MyInt; } } </code></pre> <p>This example probably isn't the best one because I think that read-only properties really shine in conjunction with a <code>readonly</code> variable, but that's beside the point. What I don't understand is why use a write-only property using the following syntax:</p> <pre><code>private int _MyInt; public int MyInt { set { _MyInt = value; } } </code></pre> <p>This is how read-only properties are described in various books and tutorials. If you set the variable, you would conceptually read it at <em>some</em> point, at least internally to the class, but to read it even internally within the class you would do so by accesssing <code>_MyInt</code> which I feel violates the spirit of encapsulation which properties try to enforce. Instead, why wouldn't you just use the full power of the property with different access modifies for accessing it as such:</p> <pre><code>private int _MyInt; public int MyInt { set { _MyInt = value; } private get { return _MyInt; } } </code></pre> <p>Which of course can just be written</p> <pre><code>public int MyInt { set; private get; } </code></pre> <p>You still get the encapsulation, but restrict other classes from access, so its still write-only to outside classes.</p> <p>Unless there is a case where you honestly would want to assign to a variable but never actually access it, in which case I would definitely be curious about when this need would arise.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload