fixed

suggest change

The fixed statement pins an object so the garbage collector does not relocate it. The collector may move objects when it compacts the heap. Unsafe pointers would be left dangling if that memory moved.

Fixed Variables

var myStr = "Hello world!";

fixed (char* ptr = myStr)
{
    // myStr is now fixed (won't be [re]moved by the Garbage Collector).
    // We can now do something with ptr.
}

Used in an unsafe context.

Fixed Array Size

unsafe struct Example
{
    public fixed byte SomeField[8];
    public fixed char AnotherField[64];
}

A fixed-size buffer can only be declared as a field of a struct, and only in an unsafe context.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents