What do you guys find preferable?
private Point location;
public int LocationX { get { return location.X; } }
public int LocationY { get { return location.Y; } }
or
private Point location;
public Point Location { get { return location; } }
The problem with the second approach is that both X and Y can be mutated by the class’ client, which in this case is not something I’d want to. Should I make a wrapper around Point so I can return an immutable Point?
Thanks
I would prefer the second approach, as the X and Y members are not mutable (Point is a structure, and thus, a value type).
That is, you cannot change the location, because you can’t modify the X or Y property of the Location property of your class, since Point is a value member. If you want to change that, you’ll have to instantiate a new Point instance.
Look at the following example, the compiler will prevent you from changing the X or Y member of the Location property: