
Examine the IL Generated by Your Code #63
Chapter 7, Help and Research
|
247
HACK
.locals init ([0] string s,
[1] string CS$00000003$00000000)
IL_0000: ldsfld string [mscorlib]System.String::Empty
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::GetBlankString2
At first glance, IL is a little confusing, and I don’t plan on trying to teach you
everything about IL in this hack. But looking at these two simple examples,
you will see one major difference. In the first method, you will see that the
IL_0000: line contains this line of code:
IL_0000: ldstr ""
And on the same line in the second method is this line of code:
IL_0000: ldsfld string [mscorlib]System.String::Empty
To understand these two lines, you first need to understand what the com-
mands
ldstr and ldsfld actually do. The IL command ldstr creates a new
string (or most likely pulls it from the string pool). The IL command
ldsfld
loads a static field from a class. Using the first string method creates a new
string object, whereas the second method simply loads the value of a static
field. To be sure, this is a tiny difference in these two procedures. You would
probably never notice the performance difference between these two meth-
ods, even if you ran the process hundreds of times. But understanding the
difference and how the .NET Framework works is what’s important. ...