Unions in C#: Structure Members Do Not Seem to be Aligned

左心房为你撑大大i 提交于 2019-12-04 03:54:45

The LayoutKind.Sequential only affects the unmanaged representation of this structure. This is because the structure contains non-blittable types (namely, strings).
If only blittable types were present, LayoutKind.Sequential would control both managed and unmanaged representation (reference).

In your case, the compiler decides to put the integer before the two strings in the managed representation (you can see that if you hover over ms while debugging and unfold STRUCT2).

You can fix that by using LayoutKind.Explicit in both STRUCT1 and STRUCT2, because Explicit affects both managed and unmanaged representations for both blittable and non-blittable types.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!