C# 12.0 introduces the InlineArrayAttribute which allows a struct to be annotated as an inline array. The array data is embedded directly in the struct, avoiding heap allocation and providing performance comparable to stack-allocated fixed-size buffers but with the safety of regular C# arrays.
Code
C#
[System.Runtime.CompilerServices.InlineArray(255)]
public struct Buffer
{
private char _element0;
}
// Data is embedded in the struct, no heap allocation
var buffer = new Buffer();
for (int i = 0; i < 255; i++)
{
buffer[i] = (char)i;
}C#
// Heap-allocated array
var buffer = new char[255];
for (int i = 0; i < 255; i++)
{
buffer[i] = (char)i;
}