【发布时间】:2013-03-02 17:39:18
【问题描述】:
我在使用结构时遇到问题。
我有这个结构:
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? -
在我看来这个问题已经涵盖了:stackoverflow.com/questions/1067340/…