Share via


컴파일러 오류 CS0212

업데이트: 2007년 11월

오류 메시지

고정되지 않은 식의 주소는 fixed 문의 이니셜라이저를 통해서만 가져올 수 있습니다.
You can only take the address of an unfixed expression inside of a fixed statement initializer

자세한 내용은 안전하지 않은 코드 및 포인터(C# 프로그래밍 가이드)를 참조하십시오.

다음 샘플에서는 고정되지 않은 식의 주소를 가져오는 방법을 보여 줍니다. 또한 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);
      // }
   }
}