来自here
// The .NET Framework 2.0 way to create a list
List<int> list1 = new List<int>();
// No boxing, no casting:
list1.Add(3);
我知道没有演员。但为什么没有boxing发生?
“3”在堆栈上,列表在堆中。
堆栈中的值如何在没有装箱的情况下移动到堆中?
引擎盖下会发生什么?
答案 0 :(得分:3)
这里没有发生拳击,因为支持列表的数组是T[]
,而不是object[]
。因此,运行时知道您的项目是整数,不需要将它们打包。
答案 1 :(得分:0)
List已经在堆上预先分配了一组int,因此它只需要将其中一个int更改为3。