I do not want to improve performance or memory usage, this question was simply caused by curiosity.
Main question
Given the following class, does the C # compiler (Mono + .NET) pack two variables shortin 4 bytes or will they consume 8 bytes (with alignment)?
public class SomeClass {
short a;
short b;
}
Secondary question
If the answer to the above question was not 4 bytes, can the following alternative offer any advantages (where it is SomeClassused in very large quantities):
public class SomeClass {
private int _ab;
public short a {
get { return _ab & 0x00ff; }
set { _ab |= value & 0x00ff;
}
public short b {
get { return _ab >> 8; }
set { _ab |= value << 8; }
}
}
source
share