> ## 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-Size Buffer

A fixed-size buffer is a contiguous block of memory allocated directly inline within a `struct` data structure, defined using the `fixed` modifier. Unlike standard C# arrays, which are reference types allocated on the managed heap, fixed-size buffers embed their elements directly into the memory layout of the containing struct. This bypasses garbage collection tracking and requires an `unsafe` context.

## Syntax and Declaration

To declare a fixed-size buffer, the containing type must be a `struct`, and the declaration must occur within an `unsafe` context.

```csharp theme={"dark"}
public unsafe struct FixedBufferStruct
{
    // Allocates 64 bytes (16 * 4 bytes) directly inside the struct
    public fixed int Data[16];
}
```

## Technical Constraints

1. **Permitted Element Types:** The element type of a fixed-size buffer is strictly limited to the following primitive types: `bool`, `byte`, `char`, `short`, `int`, `long`, `sbyte`, `ushort`, `uint`, `ulong`, `float`, or `double`. Other unmanaged types, including custom structs and pointers, are strictly prohibited. Attempting to use a pointer or custom struct will result in compiler error CS1663.
2. **Struct Confinement:** Fixed-size buffers can only be declared as instance fields within a `struct`. They cannot be declared in `class` types or as local variables.
3. **Compile-Time Sizing:** The size of the buffer must be a compile-time constant expression greater than zero.

## Memory Layout

The primary technical distinction of a fixed-size buffer is its memory footprint.

```csharp theme={"dark"}
// Standard Array Layout
public struct StandardStruct
{
    // Size: 8 bytes (on 64-bit architecture). 
    // Holds a reference pointing to an array on the managed heap.
    public int[] Data; 
}

// Fixed-Size Buffer Layout
public unsafe struct FixedStruct
{
    // Size: 40 bytes (10 elements * 4 bytes per int).
    // The data is physically located inside the struct's memory footprint.
    public fixed int Data[10]; 
}
```

Because the memory is inline, if the `struct` is allocated on the stack, the buffer is on the stack. If the `struct` is embedded in a heap-allocated `class`, the buffer is allocated inline within that object's heap memory.

## Access and Pointer Arithmetic

Accessing elements of a fixed-size buffer utilizes standard array indexing syntax, but the C# compiler translates this directly into pointer arithmetic. Bounds checking is not enforced by the runtime, making buffer overruns a potential risk.

When a struct containing a fixed-size buffer is allocated on the stack as a local variable, its memory is already unmovable. Attempting to use the `fixed` statement to pin it results in compiler error CS0213 (*"You cannot use the fixed statement to take the address of an already fixed expression"*). Instead, you assign the buffer directly to a pointer.

*(Note: If the struct resides on the managed heap as a field of a class, you must pin the class instance using the `fixed` statement before acquiring a pointer to the buffer.)*

```csharp theme={"dark"}
public unsafe void ManipulateBuffer()
{
    FixedBufferStruct instance = new FixedBufferStruct();

    // Direct index access (translated to pointer arithmetic)
    instance.Data[0] = 42;

    // Because 'instance' is a local stack variable, it is already fixed.
    // Direct pointer assignment is permitted without a 'fixed' block.
    int* ptr = instance.Data;
    
    // Pointer arithmetic
    *(ptr + 1) = 84; 
}
```

## C# 12 `InlineArray` (Safe Alternative)

In C# 12, the runtime introduced a safe, managed equivalent to the `unsafe` fixed-size buffer using the `[InlineArray]` attribute. This provides the same inline memory layout characteristics without requiring the `unsafe` keyword or pointers.

```csharp theme={"dark"}
[System.Runtime.CompilerServices.InlineArray(16)]
public struct SafeFixedBuffer
{
    // A single element defines the type; the attribute defines the length.
    private int _element0;
}

public void UseSafeBuffer()
{
    SafeFixedBuffer buffer = new SafeFixedBuffer();
    buffer[0] = 42; // Safe, bounds-checked access
}
```

<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>
