The cleaned up "new code" version with the nice access method.
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=1)]
public struct TransactionRecord {
public int OrderID;
private byte _zipCode1,
_zipCode2,
_zipCode3,
_zipCode4,
_zipCode5;
public char[] ZipCode {
get {
return new char[] {
(char)_zipCode1,
(char)_zipCode2,
(char)_zipCode3,
(char)_zipCode4,
(char)_zipCode5
};
}
}
}
I consider it broken to have to declare each byte of what in "primitive" languages is considered a fixed length string.
In use, the code just coerces chunks of a binary buffer to/from this struct by way of unsafe pointer operations. Thus the need to explicitly define the layout, charset, and word alignment rules. |