MSIL Question (basic)

Well let's 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, I understand that ldarg. # pushes arguments to a method on the stack so we can work with them? But why does it call ldarg.1 and ldarg.0 when the method takes only one argument?

+5
source share
2 answers

Instance methods have an implicit parameter ( this ), which is passed as the first argument to the method of each instance. The ldarg.0 command ldarg.0 this ldarg.0 stack. The ldarg.1 instruction is loading the first real (explicit) argument.

+11
source

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

+4
source

All Articles