Looking into the .NET code, Rectangle.Size returns new Size(Width, Height). Why did Microsoft choose this pattern? Personally, I would have thought that Size would be stored within the structure, and Rectangle.Width, for example, would return Size.Width. This would prevent a new structure from being created every property call. I’m guessing that there are some properties of immutability that influenced this decision, but I’m not sure what.
Looking into the .NET code, Rectangle.Size returns new Size(Width, Height) . Why did Microsoft
Share
Sizeis a struct, so it’s not like it’s creating a new object on the heap. It will create a new copy of aSizevalue whatever you do.I can’t see that it’s going to make much difference either way, to be honest. Given that the
WidthandHeightproperties ofSizeare inlined, I can see that there wouldn’t be much penalty from storing aSizeas you suggest… but equally I can see that the constructor forSizeis so trivial that the JIT may well be able to convert theRectangle.Sizeproperty to almost exactly the same native code.So I agree it’s a slightly odd decision, but I don’t think it’s going to hurt anyone much. Perhaps it makes serialization simpler or something like that.