IL code loads Int16 as Int32

This next C#

code:

short first = 1;
short second = 2;
bool eq1 = (first.Equals(second));

      

The code translates as:

IL_0001:  ldc.i4.1    
IL_0002:  stloc.0     // first
IL_0003:  ldc.i4.2    
IL_0004:  stloc.1     // second
IL_0005:  ldloca.s    00 // first
IL_0007:  ldloc.1     // second
IL_0008:  call        System.Int16.Equals
IL_000D:  stloc.2     // eq1

      

ldloca.s 00

- Load the address of a local variable with index indx, short form.

ldloc.1

- Load local variable 1 onto the stack.

Why are both commands not ldloca.s

, (both variables are of type short

)?

+3


source to share


1 answer


All instance methods of value types have an implicit this

type parameter ref T

, not a type T

, so a variable first

is required ldloca

. But the parameter System.Int16.Equals

has a type System.Int16

, without any ref

, so your variable second

is unnecessary (and cannot be passed from) ldloca

.



+8


source







All Articles