Point Properties in .net

What do you guys prefer?

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 Ymay be mutated class client, which in this case is not what I would like. Do I have to wrap around Pointso that I can return immutable Point?

thank

+3
source share
3 answers

, X Y ( - , , ). , X Y Location , Point . , Point.

: X Y Location:

class Program
{
    private class MyType
    {
        private readonly Point _location;

        public MyType(Point location)
        {
            _location = location;
        }

        public Point Location
        {
            get
            {
                return _location;
            }
        }
    }

    static void Main( string[] args )
    {
        var someInstance = new MyType (new Point (5, 6));

        someInstance.Location.X = 5; // <- compiler error: cannot modify the expression because it is not a variable.
    }
+6

, X, Y ,

Point ( ), . .

, , ?

, Point . \

System.Drawing.Point, . , ?

+2

The law of Demeter is not very popular in the .NET world, but if you are worried about variability, this may be a good additional justification for the first option.

0
source

Source: https://habr.com/ru/post/1759084/


All Articles