Search code examples
cilintermediate-language

ldloc var vs. ldloc.n


Do someone know if there is even a small difference using ldloc var CIL instruction and ldloc.n ?

Considering this local var table in a method scope :

.locals init ([0] int32 a,
              [1] int32 b)

Are those instructions:

ldloc.0
ldloc.1

better, worste or equal than:

ldloc a
ldloc b

Solution

  • There are many shortened forms of common opcodes with a simple goal - to shorten the length of the method. ldloc.n instructions take up one byte, ldloc.s n two bytes, and ldloc n three bytes. This is useful for inlining, as methods longer than 16 bytes are not eligible for it.