不,它在性能上不会有显着差异,但在可读性上可能会有差异。
IL
使用优化/发布(使用 LINQPad 测试)编译时,这两者都会产生相同的结果:
IL_0000: ldc.i4.5
IL_0001: stloc.0
IL_0002: ldc.i4.s 0A
IL_0004: stloc.1
IL_0005: ldloc.0
IL_0006: ldc.i4.5
IL_0007: bne.un.s IL_000D
IL_0009: ldloc.1
IL_000A: ldc.i4.3
IL_000B: pop
即使没有优化,差异也不是那么显着:
嵌套语句:
IL_0001: ldc.i4.5
IL_0002: stloc.0
IL_0003: ldc.i4.s 0A
IL_0005: stloc.1
IL_0006: ldloc.0
IL_0007: ldc.i4.5
IL_0008: ceq
IL_000A: ldc.i4.0
IL_000B: ceq
IL_000D: stloc.2
IL_000E: ldloc.2
IL_000F: brtrue.s IL_0020
IL_0011: nop
IL_0012: ldloc.1
IL_0013: ldc.i4.3
IL_0014: ceq
IL_0016: ldc.i4.0
IL_0017: ceq
IL_0019: stloc.2
IL_001A: ldloc.2
IL_001B: brtrue.s IL_001F
IL_001D: nop
IL_001E: nop
非嵌套语句:
IL_0001: ldc.i4.5
IL_0002: stloc.0
IL_0003: ldc.i4.s 0A
IL_0005: stloc.1
IL_0006: ldloc.0
IL_0007: ldc.i4.5
IL_0008: bne.un.s IL_0013
IL_000A: ldloc.1
IL_000B: ldc.i4.3
IL_000C: ceq
IL_000E: ldc.i4.0
IL_000F: ceq
IL_0011: br.s IL_0014
IL_0013: ldc.i4.1
IL_0014: nop
IL_0015: stloc.2
IL_0016: ldloc.2
IL_0017: brtrue.s IL_001B
IL_0019: nop