Possible Duplicate:
Marshal.SizeOf structure returns excessive number
As MSDN states that sizeof(bool) is 1 byte. But when I put bool in struct , sizeof struct become 4 byte. Can someone explain this behavior?
[StructLayout(LayoutKind.Sequential)]
public struct Sample1
{
public bool a1;
}
int size1 = Marshal.SizeOf(typeof (Sample1)); // 4
int size2 = sizeof (bool); // 1
You can’t directly compare
sizeofandMarshal.SizeOf. For example, if we measure it the same way, we get the same result:Presumably,
Marshalis assuming per-field alignment of 4 bytes.