Skip to content

C# Fixed Buffer

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about C# Fixed Buffer. We cover key concepts, practical examples, and best practices.

You need a struct with an inline array of fixed size — for interop, performance, or memory layout control. A regular array field is a reference (pointer) to heap memory. A fixed buffer stores the array data directly inside the struct, avoiding indirection.

Wrong

public struct PixelData
{
    public byte[] R; // Heap-allocated reference — not inline
    public byte[] G;
    public byte[] B;
}

Output: Each PixelData instance holds three references to heap arrays. The struct is 24 bytes (on 64-bit) plus heap allocation. Not contiguous.

public unsafe struct PixelData
{
    public fixed byte R[256]; // 256 bytes inline
    public fixed byte G[256]; // 256 bytes inline
    public fixed byte B[256]; // 256 bytes inline
}

Output: PixelData is 768 bytes contiguous — all data inline, no heap allocation, no indirection.

Fixed buffers require unsafe context and can only be used in struct types:

unsafe struct ColorPalette
{
    public fixed int Colors[16]; // 16 ints inline (64 bytes)
}

// Usage
var palette = new ColorPalette();
palette.Colors[0] = 0xFF0000; // Direct inline access

Prevention

  • Use fixed buffers in structs for interop with native code (matching C struct layouts).
  • Use fixed buffers for performance-critical inline storage (graphics, audio, networking).
  • Use Span<T> or regular arrays when heap allocation is acceptable.
  • Use fixed statement to get a pointer to the buffer: fixed (int* ptr = palette.Colors).
  • Use sizeof() to get the size of the inline buffer.
  • Enable AllowUnsafeBlocks in .csproj — fixed buffers require unsafe.
  • Use stackalloc for temporary stack buffers instead of fixed buffers in methods.

Common Mistakes with fixed buffer

  1. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  2. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  3. Misunderstanding that String is [Char] with poor performance for large text operations

These mistakes appear frequently in real-world CSHARP code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

Can I use fixed buffers in classes?

No. Fixed buffers are only allowed in struct types. Classes store their data on the heap, and the fixed buffer must be inline in the type layout. If you need a fixed-size buffer in a class, use a struct containing the buffer as a field.

How do I access a fixed buffer without unsafe code?

Fixed buffers are inherently unsafe — accessing them requires a fixed pointer or indexing through the implicit this. There is no safe way to access fixed buffers. Use Span<T> instead if you need safe inline buffers.

What is the memory layout of a fixed buffer?

The buffer elements are laid out sequentially at the struct offset. fixed byte[256] adds exactly 256 bytes to the struct size. The layout matches byte[256] in C — contiguous and aligned according to the element type.

Fixed buffers are used in DodaZIP for inline archive header parsing. For more C# interop patterns, visit DodaTech.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro