为什么 :
short a=0;
Console.Write(Marshal.SizeOf(a));
节目2
但是,如果我看到 IL 代码,我会看到:
/*1*/ IL_0000: ldc.i4.0
/*2*/ IL_0001: stloc.0
/*3*/ IL_0002: ldloc.0
/*4*/ IL_0003: box System.Int16
/*5*/ IL_0008: call System.Runtime.InteropServices.Marshal.SizeOf
/*6*/ IL_000D: call System.Console.Write
第 1 行的 LDC 表示:
将 0 作为int32压入堆栈。
所以肯定有4
字节被占用。
但sizeOf
显示2
字节...
我在这里想念什么?short 实际上在 mem 中占用多少字节?
我听说过填充到 4 个字节的情况,因此处理起来会更快。这里也是这样吗?
(请忽略我只是询问 2 vs 4 的 syncRoot 和 GC 根标志字节)