|
Adding to the Mix. Listing 2. This is the IL code for the foreach loop. You'll notice this is more complex than the for loop because the foreach statement creates much more overhead that can stall performance. .method private hidebysig instance void foreachDemo(class [System]System.Collections.Specialized.StringCollection outputStrings) cil managed
{
// Code size 51 (0x33)
.maxstack 2
.locals init (string V_0,
class [System]System.Collections.Specialized.StringEnumerator V_1,
class [mscorlib]System.IDisposable V_2)
IL_0000: ldarg.1
IL_0001: callvirt instance class [System]System.Collections.Specialized.StringEnumerator [System]System.Collections.Specialized.StringCollection::GetEnumerator()
IL_0006: stloc.1
.try
{
IL_0007: br.s IL_0017
IL_0009: ldloc.1
IL_000a: callvirt instance string [System]System.Collections.Specialized.StringEnumerator::get_Current()
IL_000f: stloc.0
IL_0010: ldarg.0
IL_0011: ldloc.0
IL_0012: call instance void TestLoops::Placebo(string)
IL_0017: ldloc.1
IL_0018: callvirt instance bool [System]System.Collections.Specialized.StringEnumerator::MoveNext()
IL_001d: brtrue.s IL_0009
IL_001f: leave.s IL_0032
} // end .try
finally
{
IL_0021: ldloc.1
IL_0022: isinst [mscorlib]System.IDisposable
IL_0027: stloc.2
IL_0028: ldloc.2
IL_0029: brfalse.s IL_0031
IL_002b: ldloc.2
IL_002c: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0031: endfinally
} // end handler
IL_0032: ret
} // end of method TestLoops::foreachDemo
|