tags:

views:

199

answers:

2

Well lets say we have this c# code:

public override void Write(XDRDestination destination)
{
    destination.WriteInt(intValue);
    destination.WriteBool(boolValue);
    destination.WriteFixedString(str1, 100);
    destination.WriteVariableString(str2, 100);
}

IL:

.method public hidebysig virtual instance void 
        Write(class [XDRFramework]XDRFramework.XDRDestination destination) cil managed
{
  // Code size       53 (0x35)
  .maxstack  8
  IL_0000:  ldarg.1
  IL_0001:  ldarg.0
  IL_0002:  call       instance int32 LearnIL.Test1::get_intValue()
  IL_0007:  callvirt   instance void [XDRFramework]XDRFramework.XDRDestination::WriteInt(int32)
  IL_000c:  ldarg.1
  IL_000d:  ldarg.0
  IL_000e:  call       instance bool LearnIL.Test1::get_boolValue()
  IL_0013:  callvirt   instance void [XDRFramework]XDRFramework.XDRDestination::WriteBool(bool)
  IL_0018:  ldarg.1
  IL_0019:  ldarg.0
  IL_001a:  call       instance string LearnIL.Test1::get_str1()
  IL_001f:  ldc.i4.s   100
  IL_0021:  callvirt   instance void [XDRFramework]XDRFramework.XDRDestination::WriteFixedString(string,
                                                                                                 uint32)
  IL_0026:  ldarg.1
  IL_0027:  ldarg.0
  IL_0028:  call       instance string LearnIL.Test1::get_str2()
  IL_002d:  ldc.i4.s   100
  IL_002f:  callvirt   instance void [XDRFramework]XDRFramework.XDRDestination::WriteVariableString(string,
                                                                                                    uint32)
  IL_0034:  ret
} // end of method Test1::Write

Now to the question my understanding is that ldarg.# puts the arguments supplied to the method on the stack so we can work with them? But why does it call ldarg.1 and ldarg.0 when the method only takes one argument?

+2  A: 

An instance method has the first implicit parameter this loaded by ldarg.0.

Daniel Brückner
can you extend your answer a bit? sure the first is this but is the first ldarg.1 or ldarg.0 in this case?
Petoj
+6  A: 

Instance methods have an implicit parameter (this) that is passed as the first argument of every instance method. The instruction ldarg.0 is loading this onto the stack. The instruction ldarg.1 is the loading the first real (explicit) argument.

Jason