Using Marshal.SizeOf() method on a custom struct containing only value types

前端 未结 2 522
后悔当初
后悔当初 2020-12-07 04:05

I created a simple struct which consists of two value types.

public struct Identifier
{
    public Guid ID { get; set; }
    public Byte RequestType { get; s         


        
2条回答
  •  伪装坚强ぢ
    2020-12-07 04:31

    By default the CLR is allowed to rearrange (which for simple structs it never does) and pad structs as it pleases. This is typically to keep it aligned to word boundaries when in memory.

    If you don't like this behavior and want to change it, you can specify no packing as follows:

    [StructLayout(LayoutKind.Sequential,Pack=1)]
    

提交回复
热议问题