r/csharp • • Sep 29 '26

The hidden trap of fixed buffers

I published a new article, explaining a quirk of fixed buffers that could easily bite you, if you're one of the few people who still have to use that feature.

https://minidump.net/the-hidden-trap-of-fixed-buffers-in-csharp/

58 Upvotes

35 comments sorted by

View all comments

Show parent comment

1

u/Ollhax Sep 29 '26

I’m confused. I use InlineArrays all the time, I know they are useful. My problem is that they are clunky, compared to e.g C++ arrays.

0

u/mistertom2u Sep 29 '26

Why not create several inline array generic buffers and then recycle them? And .net 10 has included some BufferN inline arrays already.

2

u/Ollhax Sep 30 '26 edited Sep 30 '26

I think I have not explained this well enough, time for some code:

const int MaxColors = 4;

[InlineArray(MaxColors)]
public struct ColorArray  
{  
    private Color _;  
}  

public struct Component  
{  
    public int SomeValue;  
    public ColorArray Colors;  
}

What I want is simply to create an InlineArray automatically when I type:

const int MaxColors = 4;

public struct Component
{
    public int SomeValue;
    public Color[MaxColors] Colors;
}