共用方式為


編譯器錯誤 CS1708

更新:2007 年 11 月

錯誤訊息

固定大小緩衝區只能透過區域變數或欄位來存取

C# 2.0 中有一項新功能,可在 struct 內部定義內嵌陣列。這類陣列只能透過區域變數或欄位來存取,而且不能當做運算式左方的中繼值來參考。此外,陣列也無法藉由 static 或 readonly 欄位來存取。

若要解決這個錯誤,請定義陣列變數,並且將內嵌陣列指派給該變數。或是從代表內嵌陣列的欄位中移除 static 或 readonly 修飾詞。

範例

下列範例會產生 CS1708:

// CS1708.cs
// compile with: /unsafe
using System;

unsafe public struct Foo
{
    public fixed char name[10];
}

public unsafe class C
{
    public Foo UnsafeMethod()
    {
        Foo myFoo = new Foo();
        return myFoo;
    }

    static void Main()
    {
        C myC = new C();
        myC.UnsafeMethod().name[3] = 'a';  // CS1708
        // Uncomment the following 2 lines to resolve:
        // Foo myFoo = myC.UnsafeMethod();
        // myFoo.name[3] = 'a';

        // The field cannot be static.
        C._foo1.name[3] = 'a';  // CS1708

        // The field cannot be readonly.
        myC._foo2.name[3] = 'a';  // CS1708
    }

    static readonly Foo _foo1;
    public readonly Foo _foo2;
}