In the IL code produced, there are some lines missing. What task does the in between lines perform?

126 views Asked by At
.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       9 (0x9)
  .maxstack  1
  .locals init ([0] class ConstReadOnly.second p)
  IL_0000:  nop
  IL_0001:  ldc.i4.5
  IL_0002:  newobj     instance void ConstReadOnly.second::.ctor(int32)
  IL_0007:  stloc.0
  IL_0008:  ret
} // end of method Program::Main

Here the IL_0002 is followed by IL_0007.
Can any one please give me the actual scenario happening here?

2

There are 2 answers

3
Paweł Łukasik On BEST ANSWER

It's only labels. There's no code in between. The label represents offset from the beginning off the function in bytes.

offset_0:  nop      //size 1 byte
offset_1:  ldc.i4.5 // size 1 byte
offset_2:  newobj instance void ConstReadOnly.second::.ctor(int32) // size 5 bytes
offset_7:  stloc.0 // size 1 byte 
offset_8:  ret

You can easily change them and recompile again.

0
svick On

When I run ildasm on your code while specifying the /BYTES option, the output is:

.method private hidebysig static void  Main(string[] args) cil managed
// SIG: 00 01 01 1D 0E
{
  .entrypoint
  // Method begins at RVA 0x2048
  // Code size       9 (0x9)
  .maxstack  1
  .locals init (class ConstReadOnly.second V_0)
  IL_0000:  /* 00   |                  */ nop
  IL_0001:  /* 1B   |                  */ ldc.i4.5
  IL_0002:  /* 73   | (06)000003       */ newobj     instance void ConstReadOnly.second::.ctor(int32)
  IL_0007:  /* 0A   |                  */ stloc.0
  IL_0008:  /* 2A   |                  */ ret
} // end of method Program::Main

Here you can see the actual bytes used to represent your code on disk as comments. Notice that all instructions use just one byte, except for the newobj instruction, which is 5 bytes, which corresponds exactly to the numbers used in the line labels.

Those labels are there so that any jump instructions can show you where they jump. They don't have any relation to lines of code you wrote in C#.