This documentation is archived and is not being maintained.

Compiler Error CS0192

A readonly field cannot be passed ref or out (except in a constructor)

A field (variable) marked with the readonly keyword cannot be passed either to a ref or out parameter except inside a constructor.

The following sample generates CS0192:

// CS0192.cs
class MyClass
{
   public readonly int TestInt = 6;

   static void TestMethod(ref int testInt)
   {
      testInt = 0;
   }

   MyClass()
   {
      TestMethod(ref TestInt);   // OK
   }

   public void PassReadOnlyRef()
   {
      TestMethod(ref TestInt);   // CS0192
   }

   public static void Main()
   {
   }
}

CS0192 also results if the readonly field is static and the constructor is not marked static.

Show: