C# Fixed Statement

C# language has a "fixed" statement that is used to create fixed size buffers in unsafe code. The fixed statement prevents the garbage collector from relocating a movable variable.

The fixed statement sets a pointer to a managed variable and "pins" that variable during the execution of the statement. Without fixed, pointers to movable managed variables would be of little use since garbage collection could relocate the variables unpredictably. The C# compiler only lets you assign a pointer to a managed variable in a fixed statement.

Here is an example

static unsafe void TestFixed()
{

    Point point = new Point();
    double[] arr = { 0, 1.5, 2.3, 3.4, 4.0, 5.9 };
    string str = "Hello World"; 

    // The following two assignments are equivalent. Each assigns the address 
    // of the first element in array arr to pointer p. 

    // You can initialize a pointer by using an array. 

    fixed (double* p = arr) { /*...*/ } 

    // You can initialize a pointer by using the address of a variable.  

    fixed (double* p = &arr[0]) { /*...*/ } 

    // The following assignment initializes p by using a string. 

    fixed (char* p = str) { /*...*/ } 

    // The following assignment is not valid, because str[0] is a char,  

    // which is a value, not a variable. 

    //fixed (char* p = &str[0]) { /*...*/ }

 

    // You can initialize a pointer by using the address of a variable, such 

    // as point.x or arr[5]. 

    fixed (int* p1 = &point.x)
    {
        fixed (double* p2 = &arr[5])
        {
            // Do something with p1 and p2.
        }
    }
}

 

References: MSDN