Skip to content

Instantly share code, notes, and snippets.

@lahma
Created October 29, 2018 08:23
Show Gist options
  • Save lahma/88de7bb0ce742952bc7a509b2cf48486 to your computer and use it in GitHub Desktop.
Save lahma/88de7bb0ce742952bc7a509b2cf48486 to your computer and use it in GitHub Desktop.
public class C {
public bool LoopAnyNonNull(string[] array) {
foreach (var x in array) {
if (x != null)
return true;
}
return false;
}
}
// produces following IL
.class public auto ansi beforefieldinit C
extends [mscorlib]System.Object
{
// Methods
.method public hidebysig
instance bool LoopAnyNonNull (
string[] 'array'
) cil managed
{
// Method begins at RVA 0x2050
// Code size 25 (0x19)
.maxstack 2
.locals init (
[0] string[],
[1] int32
)
IL_0000: ldarg.1
IL_0001: stloc.0
IL_0002: ldc.i4.0
IL_0003: stloc.1
// sequence point: hidden
IL_0004: br.s IL_0011
// loop start (head: IL_0011)
IL_0006: ldloc.0
IL_0007: ldloc.1
IL_0008: ldelem.ref
IL_0009: brfalse.s IL_000d
IL_000b: ldc.i4.1
IL_000c: ret
// sequence point: hidden
IL_000d: ldloc.1
IL_000e: ldc.i4.1
IL_000f: add
IL_0010: stloc.1
IL_0011: ldloc.1
IL_0012: ldloc.0
IL_0013: ldlen
IL_0014: conv.i4
IL_0015: blt.s IL_0006
// end loop
IL_0017: ldc.i4.0
IL_0018: ret
} // end of method C::LoopAnyNonNull
} // end of class C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment