這個例子是一個更新過的版本 。靜態函數pqr不會傳遞這個指針到棧上,但是,非靜態函數abc會把這個指針或引用傳遞到它的變量存儲 在內存中的位置。
因此,在調用函數abc之前,指令ldloc.0會把zzz的引用放到棧上。
a.cs
public class zzz
{
public static void Main()
{
pqr(10,20);
}
public static void pqr(int i , int j)
{
}
}
a.il
.assembly mukhi {}
.class private auto ansi zzz extends [mscorlib]System.Object
{
.fIEld public static initonly int32 i
.method public hidebysig static void vijay() il managed
{
.entrypoint
ldc.i4.s 10
ldc.i4.s 20
call void zzz::pqr(int32,int32)
ret
}
.method public hidebysig static void pqr(int32 i,int32 j) il managed
{
ret
}
}
調用約定指出了這些參數應該被放到棧上的順序。在IL中默認的順序是它們被寫入的順序 。因此,數字10會首先進棧,之後是數字20。
Microsoft實現了相反的順序。從而,20會首先進棧 ,之後是10。我們不能推論出這個特性。
a.cs
public class zzz
{
public static void Main()
{
bb a = new bb();
}
}
public class aa
{
public aa()
{
System.Console.WriteLine("in const aa");
}
public aa(int i)
{
System.Console.WriteLine("in const aa" + i);
}
}
public class bb : aa
{
public bb() : this(20)
{
System.Console.WriteLine("in const bb");
}
public bb(int i) : base(i)
{
System.Console.WriteLine("in const bb" + i);
}
}
a.il
.assembly mukhi {}
.class private auto ansi zzz extends [mscorlib]System.Object
{
.method public hidebysig static void vijay() il managed
{
.entrypoint
.locals (class bb V_0)
newobj instance void bb::.ctor()
stloc.0
ret
}
}
.class public auto ansi aa extends [mscorlib]System.Object
{
.method public hidebysig specialname rtspecialname instance void .ctor() il managed
{
ldarg.0
call instance void [mscorlib]System.Object::.ctor()
ldstr "in const aa"
call void [mscorlib]System.Console::WriteLine(class System.String)
ret
}
.method public hidebysig specialname rtspecialname instance void .ctor(int32 i) il managed
{
ldarg.0
call instance void [mscorlib] System.Object::.ctor()
ldstr "in const aa"
ldarga.s i
box [mscorlib]System.Int32
call class System.String [mscorlib]System.String::Concat(class System.Object,class System.Object)
call void [mscorlib]System.Console::WriteLine(class System.String)
ret
}
}
.class public auto ansi bb extends aa
{
.method public hidebysig specialname rtspecialname instance void .ctor() il managed
{
ldarg.0
ldc.i4.s 20
call instance void bb::.ctor(int32)
ldstr "in const bb"
call void [mscorlib]System.Console::WriteLine(class System.String)
ret
}
.method public hidebysig specialname rtspecialname instance void .ctor(int32 i) il managed
{
ldarg.0
ldarg.1
call instance void aa::.ctor(int32)
ldstr "in const bb"
ldarga.s i
box [mscorlib]System.Int32
call class System.String [mscorlib]System.String::Concat(class System.Object,class System.Object)
call void [mscorlib]System.Console::WriteLine(class System.String)
ret
}
}
Output
in const aa20
in const bb20
in const bb