C
C#2h ago
peppy

Use of inline arrays to model arrays of structs

For a native type with the following shape:
public struct CHRDATA {
public uint hp;
public uint mp; // ... hundreds more fields
}
public struct SAVEDATA {
public CHRDATA chr1;
public CHRDATA chr2;
public CHRDATA chr3; // ... tens more entries
}
public struct CHRDATA {
public uint hp;
public uint mp; // ... hundreds more fields
}
public struct SAVEDATA {
public CHRDATA chr1;
public CHRDATA chr2;
public CHRDATA chr3; // ... tens more entries
}
it occurred to me I could represent the sequential CHRDATAs with an InlineArray as such:
[InlineArray(80)]
public struct CHRDATA_ARRAY {
private CHRDATA _c;
}
public struct SAVEDATA {
public CHRDATA_ARRAY chrs;
}
[InlineArray(80)]
public struct CHRDATA_ARRAY {
private CHRDATA _c;
}
public struct SAVEDATA {
public CHRDATA_ARRAY chrs;
}
which raises a few questions, since I'm unfamiliar with the feature: 1) is this legal? can you make InlineArrays of blittable structs, or should usage of InlineArray be restricted to primitives? (i.e. int, float) 2) does chrs[62].hp = 300; in the above case operate on a copy or actually mutate the relevant part of SAVEDATA?
1 Reply
ティナ
ティナ2h ago
Inline arrays, or fixed sized buffers - C# feature specifications
Inline arrays provide a general-purpose and safe mechanism for declaring inline arrays within C# classes, structs, and interfaces.

Did you find this page helpful?