装箱将值类型变量转换为引用类型变量,拆箱实现反之亦然。 装箱和拆箱实现了类型系统的统一视图,其中任何类型的值都可以被视为对象。
using System;
class GFG {
// Main Method
static public void Main()
{
// assigned int value
// 2020 to num
int num = 2020;
// boxing
object obj = num;
// value of num to be change
num = 100;
System.Console.WriteLine
("Value - type value of num is : {0}", num);
System.Console.WriteLine
("Object - type value of obj is : {0}", obj);
}
}
using System;
class GFG {
// Main Method
static public void Main()
{
// assigned int value
// 23 to num
int num = 23;
// boxing
object obj = num;
// unboxing
int i = (int)obj;
// Display result
Console.WriteLine("Value of ob object is : " + obj);
Console.WriteLine("Value of i is : " + i);
}
}