public struct TestStruct
{
public int first;
public int second;
public int third;
}
Marshal.SizeOf 返回 12,这是我假设的,因为每个整数是 4 个字节。如果我将第三个更改为 double 而不是 int,我希望 Marshal.SizeOf 返回 16。确实如此。但是,如果我要添加一个双精度的第四个,Marshal.SizeOf 返回 24,而我预计是 20。我可以有 10 个 int,最终每个 int 都计为 4 个字节。但是,如果我在 3 个整数之后添加一个双精度数,则大小不是我所期望的。
public struct TestStruct //SizeOf 12
{
public int first;
public int second;
public int third;
}
public struct TestStruct //SizeOf 16
{
public int first;
public int second;
public double third;
}
public struct TestStruct //SizeOf 24, but I feel like it should be 20
{
public int first;
public int second;
public double third;
public int fourth;
}
我的想法让我误入歧途了吗?