我需要类似于的类型System.Windows.Point
,但我需要类,而不是结构。
背景
我尝试创建几个点并将它们放在列表中,并更改它们的坐标。
我的代码相当于这个:
List<Point> listofpoints = new List<Point>();
listofpoints.Add(new Point(1, 1));
listofpoints.Add(new Point(5, 5));
Line line = new Line();
line.Brush = new SolidColorBrush(Colors.Black);
line.X1 = listofpoints[0].X;
line.Y1 = listofpoints[0].X;
line.X2 = listofpoints[1].X;
line.Y2 = listofpoints[1].X;
canvas1.Children.Add(line);
// later I had to change these points coordinates
// I tried to move shapes with these points by changing only these point properties
listofpoints[0].X = 50;
listofpoints[0].Y = 50;
// but i cant, (probably) because struct is not reference type
我有的
我编写了简单的类,使我能够更改列表中的点,而无需用新点替换它们。
public class CPoint
{
public double X;
public double Y;
}
我想要的是?
我希望这个类表现得像System.Windows.Point
结构。“表现得像”我的意思是,我希望能够CPoint
像这样创建矩形:
CPoint cp1 = new CPoint();
cp1.X = 0;
cp1.Y = 0;
CPoint cp2 = new CPoint();
cp2.X = 10;
cp2.Y = 20;
Rect r = new Rect(cp1, cp2); // this is just example, in fact - I use other shapes
// i want to be able to move that rectangle by only changing cp1 and cp2 properties.
这可能吗?
我需要一些访问器吗?我知道如何制作返回某些属性的访问器,但我不知道如何编写返回整个类的访问器。