C# 结构布局和类型大小?

C# 结构布局和类型大小?,c#,layout,types,casting,C#,Layout,Types,Casting,在所有现代体系结构上,该结构是否保证为10字节 [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct NavPoint { public ushort type; public ushort px; public ushort py; public ushort rootIndex; public byte nNeighbours; public byte nEntrances;

在所有现代体系结构上,该结构是否保证为10字节

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct NavPoint
{
    public ushort type;
    public ushort px;
    public ushort py;
    public ushort rootIndex;
    public byte nNeighbours;
    public byte nEntrances;
}

是的。NET一般都有官方标准,不管用户的平台和硬件如何。Mono框架也遵循.NET框架和.NET核心的标准。为了证明您的问题,您可以使用不安全的操作员,也可以使用

var测试=新导航点{type=1,px=2,py=3,rootIndex=5,nNeighbours=0x1,nEntrances=0x2};
Console.Write(System.Runtime.InteropServices.Marshal.SizeOf(k));//10

不安全{
Console.Write(sizeof(NavPoint));//10
}