更新:2007 年 11 月

错误消息

无法向静态只读字段“identifier”的字段传递 ref 或 out(静态构造函数中除外)

如果将变量作为 ref 参数传递给一个属于静态只读字段成员的函数,则会发生此错误。由于该函数可能修改 ref 参数,所以这是不允许的。若要解决此错误,请移除字段上的 readonly 关键字,或者不要将 readonly 字段的成员传递给该函数。例如,您可能尝试创建一个可以被修改的临时变量,并将该临时变量作为 ref 参数传递,如下面的示例所示。

下面的示例生成 CS1651:

 复制代码
// CS1651.cs
public struct Inner
  {
    public int i;
  }

class Outer
{  
  public static readonly Inner inner = new Inner();
}

class D
{
   static void f(ref int iref)
   {
   }

   static void Main()
   {
      f(ref Outer.inner.i);  // CS1651
      // Try this instead:
      // int tmp = Outer.inner.i;
      // f(ref tmp);
   }
}