Developer, Former MVP, now at Microsoft - Best of 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
box
b = DirectCast(a(0), Int32)
unbox
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.IntegerType::FromObject(object)
.method public instance void Method1() cil managed{ // Code size 39 (0x27) .maxstack 2 .locals init (class [mscorlib]System.Collections.ArrayList V_0, int32 V_1) IL_0000: newobj instance void [mscorlib]System.Collections.ArrayList::.ctor() IL_0005: stloc.0 IL_0006: ldloc.0 IL_0007: ldc.i4.s 10 IL_0009: box [mscorlib]System.Int32 IL_000e: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object) IL_0013: pop IL_0014: ldloc.0 IL_0015: ldc.i4.0 IL_0016: callvirt instance object [mscorlib]System.Collections.ArrayList::get_Item(int32) IL_001b: unbox [mscorlib]System.Int32 IL_0020: ldobj [mscorlib]System.Int32 IL_0025: stloc.1 IL_0026: ret} // end of method Form1::Method1
private ArrayList ar = new ArrayList();public void Box() { ar.Add(5);}public void Unbox() { int i = (int)ar[0];}
.method public hidebysig instance void Box() cil managed{ // Code size 19 (0x13) .maxstack 8 IL_0000: ldarg.0 IL_0001: ldfld class [mscorlib]System.Collections.ArrayList BoxUnbox.Form1::ar IL_0006: ldc.i4.5 IL_0007: box [mscorlib]System.Int32 IL_000c: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object) IL_0011: pop IL_0012: ret} // end of method Form1::Box.method public hidebysig instance void Unbox() cil managed{ // Code size 19 (0x13) .maxstack 8 IL_0000: ldarg.0 IL_0001: ldfld class [mscorlib]System.Collections.ArrayList BoxUnbox.Form1::ar IL_0006: ldc.i4.0 IL_0007: callvirt instance object [mscorlib]System.Collections.ArrayList::get_Item(int32) IL_000c: unbox.any [mscorlib]System.Int32 IL_0011: pop IL_0012: ret} // end of method Form1::Unbox