编译器错误 CS1655

更新:2007 年 11 月

错误消息

无法将“variable”的字段作为 ref 或 out 参数传递,因为它是“readonly variable type”

如果您尝试将 foreach 变量、using 变量或 fixed 变量的成员作为 ref 或 out 参数传递给函数,则会发生此错误。因为这些变量在这些上下文中被视为只读变量,而这是不允许的。

下面的示例生成 CS1655:

// CS1655.cs
struct S 
{
   public int i;
}

class CMain
{
  static void f(ref int iref)
  {
  }
  
  public static void Main()
  {
     S[] sa = new S[10];
     foreach(S s in sa)
     {
        CMain.f(ref s.i);  // CS1655
     }
  }
}