Why is this code below legal
Point point = new Point();
point.X = 6;
point.Y = 5;
but this generates error?
myButton.Location.X = 6;
myButton.Location.Y = 5;
I know that structs are value type, we get copy, so we cannot modify Location.X indirectly and we have to assign to myButton.Location a whole new struct as new Point(6,5), but why does point.X = 6 work?
I don’t get the difference.
Because
pointis a variable. You’re just modifying one part of the value of the variable. That’s allowed and useful (although I’d personally steer away from using mutable value types wherever possible).So for example, you can write:
… and that will effectively just change the value of
XformyButton.Location.Changing part of a value which is copied and then lost is not useful.