我读过里希特的书中关于拳击的内容,但有一件事我不明白。
我已成功更改struct
对象。但是当我尝试更改struct
收藏时,我遇到了问题。
//my struct
internal struct Point:IChangeBoxedPoint
{
public Int32 x, y;
public void Change(Int32 x, Int32 y)
{
this.x = x;
this.y = y;
}
public override string ToString()
{
return String.Format("{0}, {1}", this.x, this.y);
}
}
public static void Main()
{
List<Point> a = new List<Point>();
Point p = new Point();
p.Change(1, 1);
Console.WriteLine(p); //write 1, 1
object o = p;
Console.WriteLine(o); //write 1, 1
((Point)o).Change(2, 2);
Console.WriteLine(o); //write 1, 1
((IChangeBoxedPoint)o).Change(3, 3);
Console.WriteLine(o); //write 3, 3
for (int i = 0; i < 10; i++)
{
p.x = p.y = i;
a.Add(p);
}
Console.WriteLine(a[0]); //write 0, 0
((IChangeBoxedPoint)a[0]).Change(300,300);
Console.WriteLine(a[0]); //still writes 0,0
}