Freigeben über


Compilerfehler CS0212

Aktualisiert: November 2007

Fehlermeldung

Sie können nur die Adresse eines unfixed-Ausdrucks innerhalb einer fixed-Anweisungsinitialisierung abrufen
You can only take the address of an unfixed expression inside of a fixed statement initializer

Weitere Informationen finden Sie unter Unsicherer Code und Zeiger (C#-Programmierhandbuch).

Im folgenden Beispiel wird veranschaulicht, wie die Adresse eines ungebundenen Ausdrucks akzeptiert wird. Im folgenden Beispiel wird CS0212 generiert.

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

Auch im folgenden Beispiel wird CS0212 generiert, und es wird eine Lösungsmöglichkeit aufgezeigt:

// 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);
      // }
   }
}