There are some paddings among fields. Padding is calculated using previous fields and next field.
Also, this condition should be true:
(size of struct) % (size of largest type) == 0
In your case the largest type is int
and its size is 4
bytes.
struct A
{
char a; // size is 2, no previous field, next field size is 2 - no alignment needed
char c; // size is 2, previous size is 2 -> 2 + 2 = 4, next size is 4 - no alignment needed
int b; //size is 4, it is last field, size is 4 + 4 = 8.
//current size is 2 + 2 + 4 = 8
//8 % 4 == 0 - true - 8 is final size
}
struct B
{
char a; // size is 2, next size is 4, alignment needed - 2 -> 4, size of this field with alignment is 4
int b; // size is 4, previous is 4, next size is 2(lower) - no alignment needed
char c; // size is 2, previous is 4 + 4 = 8 - no alignment needed
//current size is 4 + 4 + 2 = 10
//but size should be size % 4 = 0 -> 10 % 4 == 0 - false, adjust to 12
}
If you want same size for two structs you can use LayoutKind.Explicit
:
[StructLayout(LayoutKind.Explicit)]
public struct A
{
[FieldOffset(0)]
char a;
[FieldOffset(2)]
char c;
[FieldOffset(4)]
int b;
}
[StructLayout(LayoutKind.Explicit)]
public struct B
{
[FieldOffset(0)]
char a;
[FieldOffset(2)]
int b;
[FieldOffset(6)]
char c;
}
OR
You can use LayoutKind.Sequential
, Pack = 1
and CharSet = CharSet.Unicode
to get size 8.
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
public struct A
{
char a;
char c;
int b;
}
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
public struct B
{
char a;
int b;
char c;
}
Also, you can get struct size without unsafe
:
Console.WriteLine(System.Runtime.InteropServices.Marshal.SizeOf(typeof(A)));
Console.WriteLine(System.Runtime.InteropServices.Marshal.SizeOf(typeof(B)));