Edit

Share via


Compiler Error CS1666

You cannot use fixed size buffers contained in unfixed expressions. Try using the fixed statement.

This error occurs if you use the fixed-size buffer in an expression involving an instance that is not itself fixed. The runtime is free to move the unfixed instance around to optimize memory access, which could lead to errors when using the fixed-size buffer. To avoid this error, use the fixed statement.

Example

The following sample generates CS1666.

// CS1666.cs  
// compile with: /unsafe /target:library  
unsafe struct S  
{  
   public fixed int buffer[1];  
}  
  
unsafe class Test  
{  
   S field = new S();  
  
   private bool example1()  
   {  
      return (field.buffer[0] == 0);   // CS1666 error  
   }  
  
   private bool example2()  
   {  
      // OK  
      fixed (S* p = &field)  
      {  
         return (p->buffer[0] == 0);  
      }  
   }  
  
   private bool example3()  
   {  
      S local = new S();  
      return (local.buffer[0] == 0);
   }
}