3

From here

// The .NET Framework 2.0 way to create a list
List<int> list1 = new List<int>();

// No boxing, no casting:
list1.Add(3);    

I understand there is no casting. But why no boxing happens?

"3" is on stack and list is in heap.

How it happens that value from stack moved to heap without boxing?

What happens under the hood?

4

2 回答 2

3

装箱不会在这里发生,因为支持 List 的数组是T[],而不是object[]。因此,运行时知道您的项目是整数并且不需要将它们装箱。

于 2013-10-05T23:02:44.990 回答
0

List 已经在堆上预先分配了一个整数数组,因此只需将其中一个整数更改为 3。

于 2013-10-05T23:03:38.250 回答