我在VS2010 beta2中玩F#,因为我是F#的新手,所以我选择了一个常见的例子并继续实现了一个阶乘函数:
let rec factorial n =
if n <= 1 then 1 else n * factorial (n - 1);;
Run Code Online (Sandbox Code Playgroud)
如果我构建它并查看Reflector中生成的代码,我会得到相应的C#代码:
public static int Factorial(int n) {
if (n <= 1)
return 1;
return n * Factorial(n - 1);
}
Run Code Online (Sandbox Code Playgroud)
因此,如果我编译Reflector的F#代码的C#表示,我希望得到相同的IL.
但是,如果我在发布模式下编译这两个片段并比较生成的IL,它们是不同的(它们在功能上是相同的,但仍然有所不同).
C#实现编译为:
.method public hidebysig static int32 Factorial(int32 n) cil managed
{
.maxstack 8
L_0000: ldarg.0
L_0001: ldc.i4.1
L_0002: bgt.s L_0006
L_0004: ldc.i4.1
L_0005: ret
L_0006: ldarg.0
L_0007: ldarg.0
L_0008: ldc.i4.1
L_0009: sub
L_000a: call int32 TestApp.Program::Factorial(int32)
L_000f: mul …
Run Code Online (Sandbox Code Playgroud)