共用方式為


編譯器錯誤 CS1651

更新: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);
   }
}