Search code examples
.netcilcode-size

.NET CIL Code Size


When looking at the compiled CIL, I notice the code size is included in the compiled CIL. It is commented out. Below is an example

C#:

static void MakeACar()
{
Car myCar = new Car();
}

CIL:

.method public hidebysig static void MakeAObject() cil managed
{
    //Code size 7 (0x7)
    .maxstack 1
    .locals init ([0] class SimpleGC.Car c)
    IL_0000: newobj instance void SimpleGC.Car::.ctor()
    IL_0005: stloc.O
    IL_0006: ret
}

What does the code size represent?


Solution

  • This is just the number of bytes occupied by the CIL in it's bytecode form.

    (Take a look at your example: You can see that the last instruction (ret) begins at byte offset 6 (IL_0006:). Since ret is encoded as a one-byte opcode, the bytecode stream ends up having a total length of 6 + 1 = 7 bytes.)