ozbear <oz****@bigpond.comwrote:
Which is a shame. I had written what I thought was an
immutable class but missed something which rendered it
mutable. It would be nice if there were an attribute
or other decoration that one could put on a class
definition that stated it was (supposed to be) immutable.
Enforcement of that decoration would, I believe, not be difficult for
the compiler. All fields readonly so they could only
be set in a constructor would be adequate.
I agree that it's a shame that there isn't more support for
immutability - but I disagree that it's as simple as making all the
fields readonly:
public class SupposedlyImmutable
{
private readonly List<stringnames = new List<string>();
public void AddName(string name)
{
names.Add(name);
}
}
Eric Lippert has a great series of posts about immutability:
blogs.msdn.com/ericlippert/archive/tags/Immutability/default.aspx
--
Jon Skeet - <sk***@pobox.com>
Web site:
http://www.pobox.com/~skeet
Blog:
http://www.msmvps.com/jon.skeet
C# in Depth:
http://csharpindepth.com