共用方式為


編譯器錯誤 CS0192

更新:2007 年 11 月

錯誤訊息

不能傳遞靜態唯讀欄位 'name' 的欄位給 ref 或 out (除非在靜態建構函式中)

readonly 關鍵字標示的欄位 (變數),不可傳遞至 refout 參數,除非是在建構函式 (Constructor) 中。如需詳細資訊,請參閱欄位 (C# 程式設計手冊)

如果 readonly 欄位為 static,而且建構函式不是標記為 static 時,也可能會產生 CS0192。

範例

下列範例會產生 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()
    {
    }
}