class test { static void Main(string[] args) { string fileName = null; int i = 0; while ((fileName = PopQueue()) != null) { i++; } } static string PopQueue() { return null; } }Ildasm gives this:look at instruction IL_000d: it branches if the return of popqueue() is not null
.method private hidebysig static void Main(string[] args) cil managed { .entrypoint // Code size 16 (0x10) .maxstack 2 .locals init ([0] int32 i) IL_0000: ldc.i4.0 IL_0001: stloc.0 IL_0002: br.s IL_0008 IL_0004: ldloc.0 IL_0005: ldc.i4.1 IL_0006: add IL_0007: stloc.0 IL_0008: call string test::PopQueue() IL_000d: brtrue.s IL_0004 IL_000f: ret } // end of method test::MainNow with ""
class test { static void Main(string[] args) { string fileName = null; int i = 0; while ((fileName = PopQueue()) != "") { i++; } } static string PopQueue() { return null; } }ildasm: look at
.method private hidebysig static void Main(string[] args) cil managed { .entrypoint // Code size 26 (0x1a) .maxstack 2 .locals init ([0] int32 i) IL_0000: ldc.i4.0 IL_0001: stloc.0 IL_0002: br.s IL_0008 IL_0004: ldloc.0 IL_0005: ldc.i4.1 IL_0006: add IL_0007: stloc.0 IL_0008: call string test::PopQueue() IL_000d: ldstr "" IL_0012: call bool [mscorlib]System.String::op_Inequality(string, string) IL_0017: brtrue.s IL_0004 IL_0019: ret } // end of method test::MainOne may argue that in your example the difference will be about zilch. And they would be right. But I look at it as a habit. Why not reach for the faster way - the cost of programming is the same
class test { static void Main(string[] args) { int i; int[] array = new int[20]; for (i = 0; i < array.Length; i++) { } } }ildasm: comments inside
.method private hidebysig static void Main(string[] args) cil managed { .entrypoint // Code size 23 (0x17) .maxstack 2 .locals init ([0] int32 i, [1] int32[] 'array') IL_0000: ldc.i4.s 20 IL_0002: newarr [mscorlib]System.Int32 IL_0007: stloc.1 IL_0008: ldc.i4.0 IL_0009: stloc.0 IL_000a: br.s IL_0010 IL_000c: ldloc.0 IL_000d: ldc.i4.1 IL_000e: add IL_000f: stloc.0 IL_0010: ldloc.0 // load i IL_0011: ldloc.1 // load array IL_0012: ldlen // call length of array IL_0013: conv.i4 // convert to int32 ??? IL_0014: blt.s IL_000c // if < goto 000c: IL_0016: ret } // end of method test::Mainloop towards zero
class test { static void Main(string[] args) { int i; int[] array = new int[20]; for (i = array.Length; --i >= 0; ) { } } }ildasm
.method private hidebysig static void Main(string[] args) cil managed { .entrypoint // Code size 21 (0x15) .maxstack 2 .locals init ([0] int32 i, [1] int32[] 'array') IL_0000: ldc.i4.s 20 IL_0002: newarr [mscorlib]System.Int32 IL_0007: stloc.1 IL_0008: ldloc.1 IL_0009: ldlen // only done once IL_000a: conv.i4 // only done once IL_000b: stloc.0 IL_000c: ldloc.0 IL_000d: ldc.i4.1 // load constant 1 IL_000e: sub // ie --i IL_000f: dup IL_0010: stloc.0 // store in i IL_0011: ldc.i4.0 // load zero IL_0012: bge.s IL_000c // if ( i >= 0 ) goto 000c IL_0014: ret } // end of method test::MainHave a look at this - I liked it ( I think I posted the link before) http://msdn.microsoft.com/en-us/library/ms973852.aspx