I know that mutable structs are evil. But consider the following struct
:
public struct Foo
{
private readonly StringBuilder _sb;
public Foo(StringBuilder sb)
{
_sb = sb;
}
public StringBuilder Sb
{
get { return _sb; }
}
}
To me, Foo
seems immutable because when you do var foo = new Foo(new StringBuilder())
, you cannot reassign the Sb
property as it is read-only.
However you can do:
var foo = new Foo(new StringBuilder());
foo.Sb.Append("abc"); // _sb is also modified
which does affect the internal state of foo
.
Question: Is this considered as a good struct
design or should I probably avoid having mutable reference type members in a value type?