using System; namespace StructApp { /// /// BoxAndUnBox 的摘要说明。 /// public class BoxAndUnBox { public BoxAndUnBox() { // // TODO: 在此处添加构造函数逻辑 // } ///////////////////////////////////////////////////////////////////////////////////// static void Main(string[] args) { double dubBox = 77.77; /// 定义一个值形变量 object objBox = dubBox; /// 将变量的值装箱到 一个引用型对象中 Console.WriteLine("The Value is '{0}' and The Boxed is {1}",dubBox,objBox.ToString()); } ///////////////////////////////////////////////////////////////////////////////////// } } |
.method private hidebysig static void Main(string[] args) cil managed { .entrypoint // 代码大小 40 (0x28) .maxstack 3 .locals init ([0] float64 dubBox, [1] object objBox) IL_0000: ldc.r8 77.769999999999996 IL_0009: stloc.0 IL_000a: ldloc.0 IL_000b: box [mscorlib]System.Double IL_0010: stloc.1 IL_0011: ldstr "The Value is '{0}' and The Boxed is {1}" IL_0016: ldloc.0 IL_0017: box [mscorlib]System.Double IL_001c: ldloc.1 IL_001d: callvirt instance string [mscorlib]System.Object::ToString() IL_0022: call void [mscorlib]System.Console::WriteLine(string, object, object) IL_0027: ret } // end of method BoxAndUnBox::Main |
using System; namespace StructApp { /// /// BoxAndUnBox 的摘要说明。 /// public class BoxAndUnBox { public BoxAndUnBox() { // // TODO: 在此处添加构造函数逻辑 // } ///////////////////////////////////////////////////////////////////////////////////// static void Main(string[] args) { double dubBox = 77.77; object objBox = dubBox; double dubUnBox = (double)objBox; /// 将引用型对象拆箱 ,并返回值 Console.WriteLine("The Value is '{0}' and The UnBoxed is {1}",dubBox,dubUnBox); } ///////////////////////////////////////////////////////////////////////////////////// } } |
.method private hidebysig static void Main(string[] args) cil managed { .entrypoint // 代码大小 48 (0x30) .maxstack 3 .locals init ([0] float64 dubBox, [1] object objBox, [2] float64 dubUnBox) IL_0000: ldc.r8 77.769999999999996 IL_0009: stloc.0 IL_000a: ldloc.0 IL_000b: box [mscorlib]System.Double IL_0010: stloc.1 IL_0011: ldloc.1 IL_0012: unbox [mscorlib]System.Double IL_0017: ldind.r8 IL_0018: stloc.2 IL_0019: ldstr "The Value is '{0}' and The UnBoxed is {1}" IL_001e: ldloc.0 IL_001f: box [mscorlib]System.Double IL_0024: ldloc.2 IL_0025: box [mscorlib]System.Double IL_002a: call void [mscorlib]System.Console::WriteLine(string, object, object) IL_002f: ret } // end of method BoxAndUnBox::Main |
using System; namespace StructApp { /// /// BoxAndUnBox 的摘要说明。 /// public class BoxAndUnBox { public BoxAndUnBox() { // // TODO: 在此处添加构造函数逻辑 // } /////////////////////////////////////////////////////////////////// static void Main(string[] args) { double dubBox = 77.77; object objBox = dubBox; double dubUnBox = (double)objBox; object objUnBox = dubUnBox; Console.WriteLine("The Value is '{0}' and The UnBoxed is {1}",objBox,objUnBox); } /////////////////////////////////////////////////////////////////// } } |
.method private hidebysig static void Main(string[] args) cil managed { .entrypoint // 代码大小 45 (0x2d) .maxstack 3 .locals init ([0] float64 dubBox, [1] object objBox, [2] float64 dubUnBox, [3] object objUnBox) IL_0000: ldc.r8 77.769999999999996 IL_0009: stloc.0 IL_000a: ldloc.0 IL_000b: box [mscorlib]System.Double IL_0010: stloc.1 IL_0011: ldloc.1 IL_0012: unbox [mscorlib]System.Double IL_0017: ldind.r8 IL_0018: stloc.2 IL_0019: ldloc.2 IL_001a: box [mscorlib]System.Double IL_001f: stloc.3 IL_0020: ldstr "The Value is '{0}' and The UnBoxed is {1}" IL_0025: ldloc.1 IL_0026: ldloc.3 IL_0027: call void [mscorlib]System.Console::WriteLine(string, object, object) IL_002c: ret } // end of method BoxAndUnBox::Main |