更新:2007 年 11 月
错误消息
无法向只读字段“identifier”的成员传递 ref 或 out 参数(构造函数中除外)
如果将变量作为 ref 或 out 参数传递给一个属于 readonly 字段的成员的函数,则会发生此错误。由于该函数可能修改 ref 和 out 参数,所以这是不允许的。若要解决此错误,请移除字段上的 readonly 关键字,或者不要将 readonly 字段的成员传递给该函数。例如,您可能尝试创建一个可以被修改的临时变量,并将该临时变量作为 ref 参数传递,如下面的示例所示。
示例
下面的示例生成 CS1649:
// CS1649.cs
public struct Inner
{
public int i;
}
class Outer
{
public readonly Inner inner = new Inner();
}
class D
{
static void f(ref int iref)
{
}
static void Main()
{
Outer outer = new Outer();
f(ref outer.inner.i); // CS1649
// Try this code instead:
// int tmp = outer.inner.i;
// f(ref tmp);
}
}