Share via


Compiler Error CS1655

Cannot pass fields of 'variable' as a ref or out argument because it is a 'readonly variable type'

This error occurs if you are attempting to pass a member of a foreach variable, a using variable, or a fixed variable to a function as a ref or out argument. Because these variables are considered read-only in these contexts, this is not allowed.

The following sample generates 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
     }
  }
}