共用方式為


編譯器錯誤 CS0212

更新:2007 年 11 月

錯誤訊息

您只能取得 fixed 陳述式初始設定式中 unfixed 運算式的位址

如需詳細資訊,請參閱 Unsafe 程式碼和指標 (C# 程式設計手冊)

下列範例將示範如何取得 unfixed 運算式的位址。下列範例會產生 CS0212:

// CS0212a.cs
// compile with: /unsafe /target:library

public class A {
   public int iField = 5;
   
   unsafe public void M() { 
      A a = new A();
      int* ptr = &a.iField;   // CS0212 
   }

   // OK
   unsafe public void M2() {
      A a = new A();
      fixed (int* ptr = &a.iField) {}
   }
}

下列範例也會產生 CS0212,並顯示如何解決錯誤:

// CS0212b.cs
// compile with: /unsafe /target:library
using System;

public class MyClass
{
   unsafe public void M()
   {
      // Null-terminated ASCII characters in an sbyte array 
      sbyte[] sbArr1 = new sbyte[] { 0x41, 0x42, 0x43, 0x00 };
      sbyte* pAsciiUpper = &sbArr1[0];   // CS0212
      // To resolve this error, delete the previous line and 
      // uncomment the following code:
      // fixed (sbyte* pAsciiUpper = sbArr1)
      // {
      //    String szAsciiUpper = new String(pAsciiUpper);
      // }
   }
}