学习笔录-ref
今天在 WekeRoad.ActionPack里面看到如下
private static void ApplyConfig(System.Collections.Specialized.NameValueCollection config, ref string parameterValue, string configName)
{
if (config[configName] != null)
{
parameterValue = config[configName].ToString();
}
}
-------------------
于是我做了测试
{
Console.Write(s);
}
.method public hidebysig static void s(string s) cil managed
{
// 代码大小 9 (0x9)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.0=Loads the argument at index 0 onto the evaluation stack.//--加载 这个参数 他的索引是0在堆栈上
IL_0002: call void [mscorlib]System.Console::Write(string)
IL_0007: nop
IL_0008: ret
} // end of method MyProcessClass::s
{
Console.WriteLine(s);
}
.method public hidebysig static void s1(string& s) cil managed
{
// 代码大小 10 (0xa)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldind.ref=Loads an object reference as a type O (object reference) onto the evaluation stack indirectly
//--加载一个对象的引用他的类型被看作是跟0一样的 这个类型(对象引用) 在 这个堆栈上
IL_0003: call void [mscorlib]System.Console::WriteLine(string)
IL_0008: nop
IL_0009: ret
} // end of method MyProcessClass::s1
- -这样多做一步对引用类型来说没什么好处哈哈..我估计这里写错了呵呵
--在做压力测试的时候-两种写发所用时间相同- -0
----------------------------于是我对值类型的又测试了下
public static void s123(cc1 c)
{
Console.WriteLine(c.c);
}
.method public hidebysig static void s123(valuetype Process_Sample.MyProcessClass/cc1 c) cil managed
{
// 代码大小 15 (0xf)
.maxstack 8
IL_0000: nop
IL_0001: ldarga.s c =Load an argument address, in short form, onto the evaluation stack.
ldarga.s xx,即,读出参数xx的地址并压栈
IL_0003: ldfld int32 Process_Sample.MyProcessClass/cc1::c
IL_0008: call void [mscorlib]System.Console::WriteLine(int32)
IL_000d: nop
IL_000e: ret
} // end of method MyProcessClass::s123
{
Console.WriteLine(c.c);
}
.method public hidebysig static void s1234(valuetype Process_Sample.MyProcessClass/cc1& c) cil managed
{
// 代码大小 14 (0xe)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.0//-//--加载 这个参数 他的索引是0在堆栈上
IL_0002: ldfld int32 Process_Sample.MyProcessClass/cc1::c
IL_0007: call void [mscorlib]System.Console::WriteLine(int32)
IL_000c: nop
IL_000d: ret
} // end of method MyProcessClass::s1234
//--如果我没有翻译错的话
那么方法1不如方法2直接就可以使用参数,而必须读出这个参数有点像加载
且压力测试中ref总是速度快- -
---最后是关于上面两个方法的调用
s123(c22);
IL_0033: ldloc.2//--ldloc.2=Loads the local variable at index 2 onto the evaluation stack.
//--加载变量
IL_0034: call void Process_Sample.MyProcessClass::s123(valuetype Process_Sample.MyProcessClass/cc1)
IL_0039: nop
s1234(ref c22);
IL_003a: ldloca.s c22 //--ldloca.s=Loads the address of the local variable at a specific index onto the evaluation stack, short form.--加载地址
IL_003c: call void Process_Sample.MyProcessClass::s1234(valuetype Process_Sample.MyProcessClass/cc1&)
IL_0041: nop