Hi,
I'm curious why can't property in read only struc be set while read only class can be set ?
public struct A {
int C;
public int X { get { return C; } set { C = value; } }
}
public class B
{
int C;
public int X { get { return C; } set { C = value; } }
}
public static class C{
public static readonly A Ax = new A();
public static readonly B Bx = new B();
public static void Main() {
Ax.X = 10; //Got error here.
Bx.X = 10;
}
}
解决方案
Even when you change the state of a value type, you change the object identity.
Be very sure you want a value type - they work in a very non-intuitive way.