一个本地函数的代码被创建了多少次?只调用一次还是随时调用父函数



现代C#支持嵌套函数的定义。例如:

public bool Test(params int[] args) {
bool isNumberValid(int i) {
return i > 0;
}
foreach(var n in args) {
if(!isNumberValid(n)) {
return false;
}
}
return true;
}

我写上面的例子只是作为一个测试用例场景,它是否可以重构并不重要。现在的问题是,isNumberValid函数创建了多少次?它是否只创建过一次,从而被编译器移到父函数块之外?还是在调用父函数(作用域在父堆栈下(的任何时候都会在运行时重新创建?

如果您使用反编译器来检查输出,您将看到如下内容:

public bool Test(params int[] args)
{
bool flag;
int[] numArray = args;
int num = 0;
while (true)
{
if (num >= (int)numArray.Length)
{
flag = true;
break;
}
else if (Program.<Test>g__isNumberValid|1_0(numArray[num]))
{
num++;
}
else
{
flag = false;
break;
}
}
return flag;
}

这表明它已被编译为一个单独的方法,并且只编译了一次。

还要注意,对于本例中这样一个小的本地函数,JIT编译器可能会内联该函数,这样就不会对其进行函数调用

这是ILSpy生成的MSIL代码。

正如我们所看到的,本地方法被转换为与当前类的任何实例方法一样,并且代码只存在一次。

因此,编译器";重构";它本身

C#本地方法与C和C++内联函数不同,后者的代码根据使用情况而重复。

本地方法允许重构代码并创建更干净的代码,同时禁止从包含它们的方法外部进行调用,隔离处理和/或多次调用它们。

以前也有使用Action和Func的本地Lambda。

也是代理人,是所有人的祖先。

本地功能(C#编程指南(

.method public hidebysig static bool Test (int32[] args) cil managed 
{
.param [1]
.custom instance void [mscorlib]System.ParamArrayAttribute::.ctor() = (01 00 00 00)
.maxstack 2
.locals init (
[0] int32[],
[1] int32,
[2] int32 n,
[3] bool,
[4] bool
)
//  foreach (int i2 in args)
//  {
//      if (!isNumberValid(i2))
//      {
//          return false;
//      }
//  }
IL_0003: ldarg.0
// (no C# code)
IL_0004: stloc.0
IL_0005: ldc.i4.0
IL_0006: stloc.1
IL_0007: br.s IL_0026
// loop start (head: IL_0026)
IL_0009: ldloc.0
IL_000a: ldloc.1
IL_000b: ldelem.i4
IL_000c: stloc.2
// if (!isNumberValid(i2))
IL_000e: ldloc.2
>>>>>>>>>>
IL_000f: call bool ConsoleApp.Program::'<Test>g__isNumberValid|30_0'(int32)
>>>>>>>>>>
IL_0014: ldc.i4.0
IL_0015: ceq
IL_0017: stloc.3
IL_0018: ldloc.3
// (no C# code)
IL_0019: brfalse.s IL_0021
// return false;
IL_001c: ldc.i4.0
IL_001d: stloc.s 4
// (no C# code)
IL_001f: br.s IL_0031
IL_0022: ldloc.1
IL_0023: ldc.i4.1
IL_0024: add
IL_0025: stloc.1
IL_0026: ldloc.1
IL_0027: ldloc.0
IL_0028: ldlen
IL_0029: conv.i4
IL_002a: blt.s IL_0009
// end loop
// return true;
IL_002c: ldc.i4.1
IL_002d: stloc.s 4
// (no C# code)
IL_002f: br.s IL_0031
IL_0031: ldloc.s 4
IL_0033: ret
} // end of method Program::Test

这是现在作为类方法的本地方法:

.method assembly hidebysig static bool '<Test>g__isNumberValid|30_0' (int32 i) cil managed 
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor()
= (01 00 00 00)
// Method begins at RVA 0x342c
// Code size 10 (0xa)
.maxstack 2
.locals init (
[0] bool
)
// return i > 0;
IL_0001: ldarg.0
IL_0002: ldc.i4.0
IL_0003: cgt
IL_0005: stloc.0
// (no C# code)
IL_0006: br.s IL_0008
IL_0008: ldloc.0
IL_0009: ret
} // end of method Program::'<Test>g__isNumberValid|30_0'

最新更新