简体   繁体   中英

C# Accessor auto-property not sufficient for FillPolygon()

I'm creating a simply User Control. I have 4 Points and am trying to fill the area of the points using e.Graphics.FillPolygon(brush, shape);

shape is created using Point[] shape = { Target, PointB, PointC, PointD };

Those points come from the following:

    Point target = new Point(0, 0);
    public Point Target {
        get { return target; }
        set { target = value; }
    }

    Point pointB = new Point(100, 0);
    public Point PointB { get; set; }
    //    get { return pointB; }
    //    set { pointB = value; }
    //}


    Point pointC = new Point(0, 100);
    public Point PointC {
        get { return pointC; }
        set { pointC = value; }
    }


    Point pointD = new Point(200, 500);
    public Point PointD {
        get { return pointD; }
        set { pointD = value; }
    }

My problem is that using public Point PointB { get; set; } public Point PointB { get; set; } public Point PointB { get; set; } seems to not be working, so instead I have to write out the entire get { return pointB; } set { pointB = value; } get { return pointB; } set { pointB = value; }

Is there something peculiar about the shorthand notation?

Using { get; set; } { get; set; } { get; set; } : 在此处输入图片说明

Using the longer (proper?) notation: 在此处输入图片说明

It appears as though it just ignores PointB when using the shorthand notation.



Also, is it proper to have Point target = new Point(0, 0); before or after the Accessor bit:

Point target = new Point(0, 0);
public Point Target {
    get { return target; }
    set { target = value; }
}

or

public Point Target {
    get { return target; }
    set { target = value; }
}
Point target = new Point(0, 0);

The initialization should look like this (newer C# versions, C# 6.0 or higher):

public Point PointB { get; set; } = new Point(100, 0);

When you do:

Point pointB = new Point(100, 0);     // Never used, not a backing field.
public Point PointB { get; set; }     // Has a backing field that you cannot refer to.

the private pointB is never used. The backing field of the auto-property is not called pointB . It has an unusable name, and you can never reach it without using the property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM