Boxing vs Unboxing

前端 未结 6 2024
暖寄归人
暖寄归人 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 16:50

    Here's another interesting variation that supports Stefan's comments:

            int i = 2;
            object a = i; // Boxing
            object b = a; // Referencing same address on heap as 'a', b == a
    
            b = i; // New boxing on heap with reference new address, b != a
    

提交回复
热议问题