我在使用结构时遇到问题。
我有这个结构:
struct MyStruct
{
public int x;
public int y;
public MyStruct(int x,int y)
{
this.x = x;
this.y = y;
}
}
当我尝试将此结构添加到这样的列表中时:
List<MyStruct> myList = new List<MyStruct>();
// Create a few instances of struct and add to list
myList.Add(new MyStruct(1, 2));
myList.Add(new MyStruct(3, 4));
myList[1].x = 1;//<=====Compile-time error!
我收到此错误:
Compile-time error: Can't modify '...' because it's not a variable
为什么我会收到此错误以及如何解决?