p; try
{
if (a > b)
return a;
return b;
}
catch
{
return -1;
}
}
static int Test2(int a, int b)
{
if (a > b)
return a;
return b;
}
使用ILDasm工具查看,IL代码分别如下:(这里之所以引入IL,是因为IL是比较接近机器汇编,所以在IL中我们可以更清楚的了解代码的执行情况,对IL没有兴趣的可以跳过此节)
.method private hidebysig static int32 Test1(int32 a,
int32 b) cil managed
{
// 代码大小 30 (0x1e)
.maxstack 2
.locals init ([0] int32 CS$1$0000,
bool CS$4$0001)
IL_0000: nop
.try
{
IL_0001: nop
IL_0002: ldarg.0
IL_0003: ldarg.1
IL_0004: cgt
IL_0006: ldc.i4.0
IL_0007: ceq
IL_0009: stloc.1
IL_000a: ldloc.1
IL_000b: brtrue.s IL_0011
IL_000d: ldarg.0
IL_000e: stloc.0
IL_000f: leave.s IL_001b
IL_0011: ldarg.1
IL_0012: stloc.0
IL_0013: leave.s IL_001b
} // end .try
catch [mscorlib]System.Object
{
IL_0015: pop
IL_0016: nop
IL_0017: ldc.i4.m1
IL_0018: stloc.0
IL_0019: leave.s IL_001b
} // end handler
IL_001b: nop
IL_001c: ldloc.0
IL_001d: ret
} // end of method Program::Test1
Tes