As expected, this routine also starts with a definition of local variables. Here
there are three local variables, one integer, and two object types, LinkedList
and StringItem. The first thing this method does is it instantiates an object
of type LinkedList, and calls its constructor through the newobjinstruction
(notice that the method name .ctoris a reserved name for constructors). It
then loads the reference to this newly created object into the first local variable,
V_0, which is of course defined as a LinkedListobject. This is an excellent
example of managed code functionality. Because the local variable’s data type
is explicitly defined, and because the runtime is aware of the data type of every
element on the stack, the runtime can verify that the variable is being assigned
a compatible data type. If there is an incompatibility the runtime will throw an
exception.
The next code sequence at line IL_0006loads 1 into V_1(which is an inte-
ger) through the evaluation stack and proceeds to jump to IL_002b. At this
point the method loads two values onto the stack, 10 and the value of V_1, and
jumps back to IL_000a. This sequence is very similar to the one in Listing
12.1, and is simply a posttested loop. Apparently V_1is the counter, and it can
go up to 10. Once it is above 10 the loop terminates.
The sequence at IL_000ais the beginning of the loop’s body. Here the
method loads the string “item” into the stack, and then the value of V_1. The
value of V_1is then boxed, which means that the runtime constructs an object
that contains a copy of V_1and pushes a reference to that object into the stack.
An object has the advantage of having accurate type identification information
associated with it, so that the method that receives it can easily determine pre-
cisely which type it is. This identification can be performed using the IL
instruction isinst.
After boxing V_1, you wind up with two values on the stack: the string
itemand a reference to the boxed copy of V_1. These two values are then
passed to class library method string [mscorlib]System.String::
Concat(object, object), which takes two items and constructs a single
string out of them. If both objects are strings, the method will simply concate-
nate the two. Otherwise the function will convert both objects to strings
(assuming that they’re both nonstrings) and then perform the concatenation.
In this particular case, there is one string and one Int32, so the function will
convert the Int32to a string and then proceed to concatenate the two strings.
The resulting string (which is placed at the top of the stack when Concat
returns) should look something like “itemX”, where Xis the value of V_1.
After constructing the string, the method allocates an instance of the object
StringItem, and calls its constructor (this is all done by the newobjinstruc-
tion). If you look at the prototype for the StringItemconstructor (which is
displayed right in that same line), you can see that it takes a single parameter
of type string. Because the return value from Concatwas placed at the top
Reversing .NET 437