gpt4 book ai didi

c# - 为什么泛型减少了装箱/拆箱操作

转载 作者:行者123 更新时间:2023-11-30 22:13:20 27 4
gpt4 key购买 nike

来自 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”在栈上,列表在堆上。

为什么不装箱就把栈中的值移到堆中?

引擎盖下发生了什么?

最佳答案

这里没有装箱,因为支持列表的数组是 T[],而不是 object[]。因此,运行时知道您的项是整数并且不需要将它们装箱。

关于c# - 为什么泛型减少了装箱/拆箱操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19203547/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com