This is because of the implied [StructLayout] attached to a struct, the Pack field is the important one. It dictates how members of the structure are aligned after getting marshaled. The default value for Pack is 8. Which says that any member less or equal to 8 bytes in size is aligned. In other words, a short will be aligned to an offset that's a multiple of 2, an int to 4, a long or double to 8.
Key point is that alignment should still work when an array of the structures is created. Easier demonstrated with a simpler example:
using System;
using System.Runtime.InteropServices;
class Program {
static void Main(string[] args) {
Console.WriteLine(Marshal.SizeOf(typeof(Example)));
Console.ReadLine();
}
}
struct Example {
public int a;
public short b;
}
Output: 8
The a
member forces the size to be increased, two extra padding bytes are added to the structure to ensure that the int still aligns to an offset that's a multiple of 4 when the struct is used in an array. You can change the outcome by applying the attribute:
[StructLayout(LayoutKind.Sequential, Pack = 2)]
struct Example {
public int a;
public short b;
}
Output: 6
The a
member will now be mis-aligned when the struct is used in an array.
Back to Guid, you can't see it from the declaration but it is internally made up of a number of members. The first one is private field named _a
and it is an int. The Guid therefore requires a 4 byte alignment. So 2 extra padding bytes are needed to get your struct to align properly when it is used in an array. You'd need Pack=1 or Pack=2 on S2 to get 18 bytes.
More background on structs and the special way they are treated in .NET in this answer.