Boxing and unboxing in IL
Using ildasm.exe, we can see what boxing and unboxing look like underneath the covers. To find out, create the short C# file, box.cs, shown in listing 2.10.
Listing 2.10 Boxing and unboxing in C#
class Box {
public static void Main() {
When we compile and disassemble this file, we get the IL shown in listing 2.11.
|
.locals |
(int32 V_0, object V_1, int32 V_2) |
// |
i, o, and j |
|
IL_0000 |
ldc.i4.s 123 |
// |
load 123 |
|
IL_0002 |
stloc.0 |
// |
store in i |
|
IL_0003 |
ldloc.0 |
// |
load i |
|
IL_0004 |
box [mscorlib]System.Int32 |
// |
box i |
|
IL_0009 |
stloc.1 |
// |
store ref in o |
|
IL_000a |
ldloc.1 |
// |
load o |
|
IL_0 0 0b |
unbox [mscorlib]System.Int32 |
// |
unbox ref |
|
IL_0010 |
ldind.i4 |
// |
load value via ref |
|
IL_0011 |
stloc.2 |
// |
store in j |
The first thing we notice is that the box and unbox operations are IL primitives. Boxing and unboxing are features of the platform, not the preserve of C#. The box operation takes a value type from the top of the stack, stores it in a new reference type which it creates on the managed heap, and places a reference to the newly created type on top of the stack. In this example, that reference is then stored in V_1, or o in C#. Unboxing involves the reverse operation and leaves the unboxed value type on top of the stack. In this case, the result is stored in V_2, or j in our C# program.
2.8.3 Coding IL programs
You can write your own IL programs and assemble them using .NET's ilasm.exe assembler. Depending on your installation, you should find ilasm.exe at C:\WINNT\Microsoft.NET\Framework\<.NET Version>. Listing 2.12 presents a skeleton program that you can use as a template for your own IL programs.
Listing 2.12 A skeleton IL program
// assemble : ilasm skel.il
.assembly extern mscorlib {} // reference the core assembly .assembly 'skel' {} // this assembly name
.class Skel extends ['mscorlib']System.Object {
.method public static void
Main(class System.String[] args) cil managed {
.entrypomt .maxstack 8
// program starts here // maximum number of stack slots
// main code goes here /////////////// ldstr "IL Skeleton Program!"
call void ['mscorlib']System.Console::WriteLine(string) ret // exit
// methods go here ///////////////////// // constructor follows...
.method public hidebysig specialname rtspecialname instance void .ctor() cil managed { .maxstack 8 ldarg.0
call instance void ['mscorlib']System.Object::.ctor() ret
The program begins by accessing the .NET core assembly, mscorlib.dll, in which many of the classes in fundamental namespaces, such as System, reside. To use the skeleton for your own IL programs replace the comments to insert required local variables, the main line code, and any methods your program may require. Figure 2.15 illustrates how to assemble and execute the result.
C:\DotNet\cha_2>ilasm skel.il
Microsoft <R> .NET Framework IL Assembler. Uersion 1.0.3215.11 Copyright <C> Microsoft Corporation 1998-2001. All rights reserved. Assembling 'skel.il' , no listing file, to EXE —> 'skel.EXE' Source file is ANSI
Assembled method Skel::Main Assembled method Skel::.ctor Creating PE file
Emitting members: Global
Class 1 Methods: 2; Writing PE file
Operation completed successfully
C:\DotNet\cha_2>skel IL Skeleton Program?
Figure 2.15 Assembling and executing the skeleton IL program
Average user rating: 5 stars out of 1 votes
Post a comment