[nem-en] Initonly modifier for immutable fields

Snaury snaury at gmail.com
Sun Jul 9 20:55:22 CEST 2006


On 7/9/06, Michal Moskal <michal.moskal at gmail.com> wrote:
> The solution here is to avoid mutator methods on structs. It is the
> best for structs to be immutable. This is also the advice given by the
> C# folks, that I've read somewhere. If you're using mutable structs,
> you deserve punishment.

Sorry for getting in, but here is where you might be crossing the line
and strip features from CLI. The difference between class and struct
is that class is passed by reference, and struct is passed by value.
There is nothing bad in struct being mutable, I myself often use
structs in place of classes when I know for sure that I won't need to
pass it somewhere that will change it (this is already guaranted by
the fact it is passed by value, it some function changes it, it
changes it local copy). The methods on structs, however, still take
the reference to struct and should be able to change it, it is
especially useful when you don't expose fields and provide properties
instead. One of the most important field is, for example, P/Invoke.
The might be dozens of fields in a struct which you don't want to init
via constructor. Or the might be just a hidden uint value where you
expose its parts as properties (I did that when I was writing wrapper
for Uniscribe). It is to important for structs to be able to be
mutable, so I don't want this to be just stripped.



More information about the devel-en mailing list