I am often writing classes that create a new object in a property getter, but I have read this is not necessarily best practice. Eg:
public class Board
{
public float Width { get; }
public float Height { get; }
public CGSize Size { get { return new CGSize(this.Width, this.Height); } }
public Board(float width, float height)
{
this.Width = width;
this.Height = height;
}
}
Is there anything wrong with that?
See here: Is object creation in getters bad practice? where various upvoted people suggest it is bad practice, eg: "Yes, it is bad practice. Ideally, a getter should not be changing or creating anything". And that reading a property twice should produce identical results (whereas creating a new object will be different each time.)
(I note that in C# the System.Drawing.Rectangle class has a Size property that does return a new object each time.)