Fixed-Size buffer
Motivation
Fixed-size buffers are used to declare “C-style” in-line arrays as members of structs, and are primarily useful for interfacing with unmanaged APIs.
Constraint
- Length of fixed-size buffer may only be constant.
- Fixed-size buffer may only be declared and accessed in unsafe context.
cs
C c = new();
_ = c.Buffer[0]; // CS0214
struct C
{
public unsafe fixed byte Buffer[30];
public unsafe void M()
{
C c = new();
_ = c.Buffer[0]; // compiles
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
- May not be static. But instance of its containing type can be static.
cs
struct C
{
public static unsafe fixed byte Buffer[30];
public unsafe void M()
{
C c = new();
_ = c.Buffer[0]; // compiles
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
- Element type must be one of the following:
bool
,byte
,short
,int
,long
,char
,sbyte
,ushort
,uint
,ulong
,float
ordouble
.