> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C# Fixed Statement

The `fixed` statement prevents the garbage collector (GC) from relocating a movable variable during execution. By pinning the object in the managed heap, it allows developers to safely extract and manipulate an unmanaged pointer to the object's memory address. Without pinning, the GC's compaction phase could move the object, rendering the pointer invalid and causing memory corruption.

## Execution Context and Rules

* **Unsafe Context:** The `fixed` statement can only be used within an `unsafe` context.
* **Immutability:** The pointer variable declared in the `fixed` statement is implicitly `readonly`. It cannot be reassigned to point to a different memory address.
* **Scope:** The pinning is strictly scoped. Once execution exits the `fixed` block (or the enclosing scope for C# 8.0+ declarations), the object is unpinned and becomes eligible for GC relocation.
* **Multiple Declarations:** Multiple pointers of the same type can be declared in a single `fixed` statement. Multiple `fixed` statements can also be nested.

## Syntax and Mechanics

**Standard Block Syntax**

```csharp theme={"dark"}
unsafe
{
    byte[] buffer = new byte[1024];
    
    // Pin the array in memory
    fixed (byte* ptr = buffer)
    {
        // ptr is valid and the array is pinned within this block
        *ptr = 0xFF; 
    }
    // buffer is unpinned here
}
```

**C# 8.0+ Declaration Syntax**
C# 8.0 introduced a block-less `fixed` declaration, which pins the variable for the remainder of the enclosing scope, reducing indentation.

```csharp theme={"dark"}
unsafe void ProcessBuffer(byte[] buffer)
{
    // Pin the array for the duration of the method scope
    fixed byte* ptr = buffer;
    
    *ptr = 0xFF;
    
    // buffer is implicitly unpinned when the method returns
}
```

## Pinning Strings and Arrays

When pinning managed arrays or strings, the `fixed` statement bypasses the object header and extracts a pointer directly to the first element or character.

```csharp theme={"dark"}
unsafe
{
    string text = "CLR";
    
    // Pins the string object, but ptr points to the first char ('C')
    fixed (char* ptr = text)
    {
        // Pointer arithmetic relies on the contiguous memory of the pinned object
        char secondChar = *(ptr + 1); 
    }
}
```

## Custom Types and `GetPinnableReference`

Starting with C# 7.3, the `fixed` statement can operate on any custom type, provided the type implements a `GetPinnableReference` method. This method must return a `ref` or `ref readonly` to an unmanaged type.

```csharp theme={"dark"}
public class CustomBuffer
{
    private int[] _data = new int[100];
    
    // Enables the type to be used directly in a fixed statement
    public ref int GetPinnableReference()
    {
        return ref _data[0];
    }
}

unsafe
{
    CustomBuffer buffer = new CustomBuffer();
    
    // Implicitly calls buffer.GetPinnableReference()
    fixed (int* ptr = buffer)
    {
        *ptr = 42;
    }
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
