4
我在使用结构时遇到问题。如何解决此问题:Struct In Collection未更改
我有这样的结构:
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
为什么我我得到这个错误以及如何解决它?
为什么你使用'struct'? – 2013-03-14 11:38:10
在我看来,这个问题已经涵盖:http://stackoverflow.com/questions/1067340/c-sharp-modifying-structs-in-a-listt – 2013-03-14 11:41:24