It's said, a best practice is to make structs immutable, so I did this:
struct MyStruct
{
public int mI;
public string mS;
}
[ImmutableObject(true)]
struct ComplexStruct
{
public int mJ;
public MyStruct st;
}
And the in main:
ComplexStruct cs = new ComplexStruct
{
mJ = 6,
st = new MyStruct
{
mI = 7,
mS = "immutable"
}
};
cs.st.mS = "Changed";
Console.WriteLine(cs.st.mS);
cs.st = new MyStruct
{
mI = 8,
mS = "check immutable?"
};
It compiles and runs well. What I expected is:
cs.st is a struct, and changing the cs.st.mS is using a copy of the value, and the content should not change to "Changed"?
I used Immutable attribute on "ComplexStruct", why still after initialization, I can change the "st" member struct?
I expected that compilation should fail, or there's runtime exception, because of using "Immutable". So why they don't work?
Thanks a lot.