我有一个带有名为"rcnesw"的迭代变量的foreach语句.
foreach( RCNESW rcnesw in shiftersave )
{
TestG.ShiftQuad( rcQuads, rcnesw.row, rcnesw.col, GO_.Up);
// other code...
}
Run Code Online (Sandbox Code Playgroud)
在调试rcnesw.row和rcnesw.col CHANGE的值作为调用ShiftQuad的结果 - 我认为foreach循环变量是readonly.是不是只读了?
注意:TestG.ShiftQuad方法中还有一个foreach循环,它有自己的本地(我认为是本地)变量,它具有相同的名称和类型:RCNESW rcnesw.该值似乎传回到调用的foreach位置.
(=====编辑=====加入=====)
首先,感谢所有关于"readonly"变量的"可变"性质的信息.这澄清了情况(C#/ .NET文档没有).
有关ShiftQuad方法的更多信息,它是:
public void ShiftQuad( List<RCPair> rcList, int row, int col, int dir )
Run Code Online (Sandbox Code Playgroud)
在那个方法里面没有任何改变行或col.ShiftQuad声明自己的rcnesw:
RCNESW rcnesw = null;
Run Code Online (Sandbox Code Playgroud)
或者是吗?该声明是否不会创建RCNESW的新实例?因为当该方法使rcnesw.row和rcnesw.col发生变化时,调用方法显然会收到这些变化.
public class RCNESW
{
public int row;
public int col;
public bool bNorth;
public bool bEast;
public bool bSouth;
public bool bWest;
}
Run Code Online (Sandbox Code Playgroud)
此时我仍然认为这是一个编译器错误.不应通过值调用的变量在调用者处显示对局部变量的更改.
(==== edit ==== solve ===)这个问题实际上与本地与非本地(或按值传递)无关,搞砸了一些列表如何成为相同对象的别名.