Is there any workaround for the error CS1663 ("Fixed size buffer type must be one of the following: bool, byte, short, int, long, char, sbyte, ushort, uint, ulong, float or double.")?
I need to declare a unsafe fixed array from another blittable custom type struct but I'm stuck in this compiler error.
Showing some code to elucidate the problem below.
struct s1
{
byte _b1;
byte _b2;
}
unsafe struct s2
{
fixed s1 _s1[5]; // CS1663 here...
}
Note that the two structs are blittable, so the error doesn't make any sense for me.
Anyone have any idea about what I could do?
Thanks.
Solution for C# 12 (Dotnet 8+)
If you are able to use C# 12 (dotnet 8), there is the
System.Runtime.CompilerServices.InlineArray
attribute which allows you to create a struct to represent a fixed size array of another struct without any pointer shenanigans.Take a look:
You can then index into types with the
InlineArray
attribute like a normal array:You can read more about them here.