244
|
Chapter 7, Help and Research
#63 Examine the IL Generated by Your Code
HACK
C# Project, your code is not compiled into machine code (as a nonmanaged
C++ application would be), but instead is compiled into IL. When your
application is run, the IL of your application is then compiled by the JIT
(just-in-time) compiler and turned into machine code. This machine code
can then be executed and your application can be run.
This is an extremely quick definition of how IL, the JIT com-
piler, and the CLR work together. If you have not already
read it, you owe it to yourself to read Applied Microsoft .NET
Framework Programming (Microsoft Press). This book cov-
ers how the internals of .NET function and is an essential
read for all .NET developers.
Most of the time, you don’t have to even think about the IL that your code is
compiled into, but sometimes being able to examine this IL can be very use-
ful. Among the many reasons to look at the IL generated by your code, you
may want to:
Better understand what your code is doing
Compare the performance of different coding approaches
Compare the differences between various .NET languages
Diagnose difficult bugs
In this hack, you will learn how to examine IL to compare two different
ways of performing the same action. The .NET Framework includes a large
number of different ways to do the same things. You can add strings
together using the normal addition sign or the
StringBuilder object, you can
set a string to empty using empty double quotes (
"") or the string.Empty
property, and so on. IL presents an interesting way to view how each of
these is treated when compiled into IL.
Microsoft Intermediate Language Disassembler (ILDASM)
MSIL Disassembler (ILDASM) is an application that is included with the
Microsoft .NET Framework SDK. You don’t even need Visual Studio to use
ILDASM. (But if you don’t have Visual Studio, I really don’t know why you
bought this book.)
Using ILDASM you can examine the IL contained in your assemblies or exe-
cutables. The easiest way to launch ILDASM is to open the Visual Studio
command prompt and type in
ildasm. You can also find the file in the fol-
lowing locations:
Examine the IL Generated by Your Code #63
Chapter 7, Help and Research
|
245
HACK
Visual Studio .NET 2002
%ProgramFiles%\Microsoft Visual Studio .NET\FrameworkSDK\Bin
Visual Studio .NET 2003
%ProgramFiles%\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin
If you have only the framework SDK installed, it will be in the <SDK
Directory>\Bin directory.
You can see the ILDASM application in Figure 7-20.
This is a pretty plain window and doesn’t hint at the true potential of this
application. First, I am going to set up a small example of two different ways
to create an empty string, and then using ILDASM, I’ll determine which
method is more efficient. Here is the first method using simple empty
quotations:
public class ILDASMTest {
public string GetBlankString1( )
{
string s = "";
return s;
}
The second method uses the string.Empty field:
public string GetBlankString2( )
{
string s = string.Empty;
return s;
}
}
Now I will need to compile both of these methods into an assembly and load
that assembly into ILDASM. You can compile it at the command line with
csc
/t:library /out:ILDASMTest.dll ProgramName.cs
or create a project and
Figure 7-20. ILDASM main window
246
|
Chapter 7, Help and Research
#63 Examine the IL Generated by Your Code
HACK
compile it. To open the assembly in ILDASM, click on File Open and then
select the assembly in the file dialog. After selecting the assembly, you will see
the screen shown in Figure 7-21.
From this screen, you can see the two methods in the assembly. Double-
clicking on each of these methods will show the IL that makes up this
method. Here is the IL for the first string method:
.method public hidebysig instance string
GetBlankString1( ) cil managed
{
// Code size 12 (0xc)
.maxstack 1
.locals init ([0] string s,
[1] string CS$00000003$00000000)
IL_0000: ldstr ""
IL_0005: stloc.0
IL_0006: ldloc.0
IL_0007: stloc.1
IL_0008: br.s IL_000a
IL_000a: ldloc.1
IL_000b: ret
} // end of method Class1::GetBlankString1
And here is the IL from the second string method:
.method public hidebysig instance string
GetBlankString2( ) cil managed
{
// Code size 12 (0xc)
.maxstack 1
Figure 7-21. ILDASM assembly view

Get Visual Studio Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.