I need a small datatype that has exactly X bytes.
To archive this I have basically two ways:
- I could define a byte[]-array with a capacity of X
- I could combine some build-in numeric types to archive the number of bytes I need, f. e. for X = 12:
- I could use 3 integers
- I could use 1 long and one short
- I could use 12 byte fields
- I could ...
I have seen that in a lot of structs f. e. Guid or MongoDb's ObjectId concrete fields are used, even if they are more or less just a plain byte array.
public struct XY
{
private readonly int _a;
private readonly int _b;
private readonly int _c;
}
vs.
public struct XY
{
private readonly byte[] bytes = new byte[12]; // probably set via ctor
}
Does this provide any significant benefits (apart from usability)?
Or to put it in another way: Is using a byte array within a struct a bad idea?