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 |
原文轉自:http://www.anti-gravitydesign.com