Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Types and Literals

Inline arrays

Reference for C# inline arrays: the [InlineArray] attribute, fixed-size struct buffers, indexing, Span conversion, foreach, limits and when to use them.

An inline array is a struct that contains a fixed number of elements stored directly inside the struct, with no separate heap allocation. You declare one by putting [InlineArray(n)] on a struct that has exactly one field; the runtime lays out n copies of that field in a row. Since C# 12 you can index it, slice it as a Span<T>, and loop over it with foreach.

Use inline arrays when you need a small, fixed buffer that lives on the stack or inside another object: parsing buffers, fixed-size records, and hot paths where allocating an array would show up in profiles.

C#
[System.Runtime.CompilerServices.InlineArray(4)]
struct Buffer4
{
    private int _element0;
}

Try it

C# Declaring and using an inline array

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Generic inline array in a struct

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
[InlineArray(length)]
[public|internal] struct Name
{
    private ElementType _element0;   // exactly one instance field
}
Part Description
length Positive integer constant: number of elements.
Single field The field's type is the element type. The name does not matter, and it may be generic through a generic struct.
Indexing The compiler lets you use buffer[i] directly, with a bounds check.
Conversions Implicit conversion to Span<T> and ReadOnlySpan<T>, which also gives you Length and slicing.

Errors. The attribute requires a struct with exactly one instance field and a positive length; other shapes are rejected by the compiler. Indexing outside 0..length-1 throws IndexOutOfRangeException at run time, as the first example shows.

Usage notes

  • No built-in Length. The attribute does not add one; the example declares a const. A Span<T> conversion gives you Length for free.
  • Value semantics. Assigning an inline array copies every element, so large ones are expensive to pass; use ref, in or Span<T>.
  • Stack friendly. A local inline array needs no stackalloc or heap allocation, and its length is known statically.
  • Safe replacement for fixed buffers. Old fixed int data[4]; fields work only in unsafe structs and give pointers; inline arrays are safe and typed for any element type including references.
  • Typical users. The runtime and libraries use them for small fixed buffers and for the compiler's own helper types.
  • Not for variable size. The count is part of the type; for dynamic sizes use stackalloc with a Span<T>, ArrayPool<T> or List<T>.
  • Collection expressions can initialize a Span<T> over one, but there is no literal syntax for the struct itself.