With the release of C# 7.2 there is now the ability to have readonly
structs, which in many cases can improve performance.
For one of my structs I am using a fixed-size byte array to actually hold the data. However when I marked the struct
and the byte array field readonly
, C# compiler complained that readonly
is not valid on the field. Why can't I have both fixed
and readonly
on a field in a struct
?
readonly unsafe struct MyStruct {
readonly fixed byte _Value[6]; //The modifier 'readonly' is not valid for this item.
}
fixed
altogether for a theoretical "readonly fixed buffer" (so we can't get it as a pointer) and allow set access only in the constructor. – Virgenvirgie