Lets consider that I have a public property called AvatarSize like the following,
public class Foo { ... public Size AvatarSize { get { return myAvatarSize; } set { myAvatarSize = value; } } ... }
Now if a target class wants to set this property, then they need to do it the following way,
myFoo.AvatarSize = new Size(20, 20); //this is one possible way
But if I try to set it like this,
myFoo.AvatarSize.Height = 20; //.NET style myFoo.AvatarSize.Width = 20; //error
the compiler get me an error stating that it cannot modify the return values. I know why it happens, but I would like it to support the second way also. Please help me with a solution.
P.S. Sorry if the title is absurd
Sizeis a structure. Being a ValueType it’s immutable. If you change it’s property like that, it will only modify an object instance in the stack, not the actual field. In your caseAvatarSizeproperty can only be set using a struct constructor:new Size(20, 20).