当我使用struct
s的数组(例如System.Drawing.Point)时,我可以通过索引获取项目并对其进行更改。通过列表和数组中的索引获取struct项目
例如(此代码做工精细):
Point[] points = new Point[] { new Point(0,0), new Point(1,1), new Point(2,2) };
for (int i = 0; i < points.Length; i++)
{
points[i].X += 1;
}
但是当我使用List它不工作:
无法修改 返回值“System.Collections.Generic.List 。这[INT]” ,因为它不是一个可变
实施例(该代码不工作精细):
List<Point> points = new List<Point> { new Point(0,0), new Point(1,1), new Point(2,2) };
for (int i = 0; i < points.Count; i++)
{
points[i].X += 1;
}
我知道,当我通过索引获取列表项我得到了它的复制和编译器提示,我没有犯一个错误,但为什么要采取数组索引的元素原理不同?
http://stackoverflow.com/questions/14945118/cant-change-structs-members-value-inside-generic-collections#answer-14958238 –