This documentation is archived and is not being maintained.

Compiler Error CS0212

You can only take the address of unfixed expression inside of a fixed statement initializer

The following sample generates CS0212 and shows how to take the address of an unfixed expression:

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

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

        // Try the following code instead: 
        /*
        fixed (int* ptr = &a.iField)
        {
        }        
        */
    }
    static void Main() 
    {
    }
}

The following sample also generates CS0212 and shows how to resolve the error:

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

public class MyClass
{
   unsafe public void mf()
   {
      // 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);
      // }
   }

   public static void Main()
   {
   }
}
Show: