Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize

Compiler Error CS0192

Fields of static readonly field 'name' cannot be passed ref or out (except in a static constructor)

A field (variable) marked with the readonly keyword cannot be passed either to a ref or out parameter except inside a constructor. For more information, see Fields (C# Programming Guide).

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

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()
    {
    }
}

Community Additions

ADD
Show:
© 2015 Microsoft