Boxing vs Unboxing

前端 未结 6 1971
暖寄归人
暖寄归人 2021-02-01 16:48

Another recent C# interview question I had was if I knew what Boxing and Unboxing is. I explained that value types are on Stack and reference types on Heap. When a value is cast

6条回答
  •  生来不讨喜
    2021-02-01 17:16

    Very short: boxing means creating a new instance of a reference type. If you know this, you understand that one instance does not change by creating another.

    What you are doing with a = 2 is not changing the value in the 'box', you are creating a new instance of a reference type. So why should anything else change?

提交回复
热议问题