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);
}
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for