共用方式為


編譯器錯誤 CS1657

更新:2007 年 11 月

錯誤訊息

無法將 'parameter' 當做 ref 或 out 引數傳遞,因為 'reason''

當變數為唯讀時,將變數當做 refout 引數傳遞時,便會發生這個錯誤。唯讀內容包括 foreach 反覆運算變數、using 變數和 fixed 變數。若要解決這個錯誤,請勿呼叫使用 foreach、using 或 fixed 變數做為 using 區塊、foreach 陳述式和 fixed 陳述式中的 ref 或 out 參數的函式。

範例

下列範例會產生 CS1657:

// CS1657.cs
using System;
class C : IDisposable
{
    public int i;
    public void Dispose() {}
}

class CMain
{
    static void f(ref C c)
    {
    }
    static void Main()
    {
        using (C c = new C())
        {
            f(ref c);  // CS1657
        }
    }
}

下列程式碼說明 fixed 陳述式中的相同問題:

// CS1657b.cs
// compile with: /unsafe
unsafe class C
{
    static void F(ref int* p)
    {
    }

    static void Main()
    {
        int[] a = new int[5];
        fixed(int* p = a) F(ref p); // CS1657
    }
}