下面的C#是一个非常简单的循环,但我认为它是两个循环。我的一位同事说,他认为这是一个单一的循环。你能告诉我它是一个循环还是两个循环吗?您能否告诉我如何阅读 IL 并向我的同事证明它是两个循环?
var ints = new List<int> {1, 2, 3, 4};
foreach (var i in ints.Where(x => x != 2))
{
Console.WriteLine(i);
}
如果事实证明这实际上是一个很酷的循环。我仍然想知道如何读取 IL 并看到它只是一个循环。
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 137 (0x89)
.maxstack 3
.locals init ([0] class [mscorlib]System.Collections.Generic.List`1<int32> ints,
[1] int32 i,
[2] class [mscorlib]System.Collections.Generic.List`1<int32> '<>g__initLocal0',
[3] class [mscorlib]System.Collections.Generic.IEnumerator`1<int32> CS$5$0000,
[4] bool CS$4$0001)
IL_0000: nop
IL_0001: newobj instance void class [mscorlib]System.Collections.Generic.List`1<int32>::.ctor()
IL_0006: stloc.2
IL_0007: ldloc.2
IL_0008: ldc.i4.1
IL_0009: callvirt instance void class [mscorlib]System.Collections.Generic.List`1<int32>::Add(!0)
IL_000e: nop
IL_000f: ldloc.2
IL_0010: ldc.i4.2
IL_0011: callvirt instance void class [mscorlib]System.Collections.Generic.List`1<int32>::Add(!0)
IL_0016: nop
IL_0017: ldloc.2
IL_0018: ldc.i4.3
IL_0019: callvirt instance void class [mscorlib]System.Collections.Generic.List`1<int32>::Add(!0)
IL_001e: nop
IL_001f: ldloc.2
IL_0020: ldc.i4.4
IL_0021: callvirt instance void class [mscorlib]System.Collections.Generic.List`1<int32>::Add(!0)
IL_0026: nop
IL_0027: ldloc.2
IL_0028: stloc.0
IL_0029: nop
IL_002a: ldloc.0
IL_002b: ldsfld class [mscorlib]System.Func`2<int32,bool> ConsoleApplication1.Program::'CS$<>9__CachedAnonymousMethodDelegate2'
IL_0030: brtrue.s IL_0045
IL_0032: ldnull
IL_0033: ldftn bool ConsoleApplication1.Program::'<Main>b__1'(int32)
IL_0039: newobj instance void class [mscorlib]System.Func`2<int32,bool>::.ctor(object,
native int)
IL_003e: stsfld class [mscorlib]System.Func`2<int32,bool> ConsoleApplication1.Program::'CS$<>9__CachedAnonymousMethodDelegate2'
IL_0043: br.s IL_0045
IL_0045: ldsfld class [mscorlib]System.Func`2<int32,bool> ConsoleApplication1.Program::'CS$<>9__CachedAnonymousMethodDelegate2'
IL_004a: call class [mscorlib]System.Collections.Generic.IEnumerable`1<!!0> [System.Core]System.Linq.Enumerable::Where<int32>(class [mscorlib]System.Collections.Generic.IEnumerable`1<!!0>,
class [mscorlib]System.Func`2<!!0,bool>)
IL_004f: callvirt instance class [mscorlib]System.Collections.Generic.IEnumerator`1<!0> class [mscorlib]System.Collections.Generic.IEnumerable`1<int32>::GetEnumerator()
IL_0054: stloc.3
.try
{
IL_0055: br.s IL_0067
IL_0057: ldloc.3
IL_0058: callvirt instance !0 class [mscorlib]System.Collections.Generic.IEnumerator`1<int32>::get_Current()
IL_005d: stloc.1
IL_005e: nop
IL_005f: ldloc.1
IL_0060: call void [mscorlib]System.Console::WriteLine(int32)
IL_0065: nop
IL_0066: nop
IL_0067: ldloc.3
IL_0068: callvirt instance bool [mscorlib]System.Collections.IEnumerator::MoveNext()
IL_006d: stloc.s CS$4$0001
IL_006f: ldloc.s CS$4$0001
IL_0071: brtrue.s IL_0057
IL_0073: leave.s IL_0087
} // end .try
finally
{
IL_0075: ldloc.3
IL_0076: ldnull
IL_0077: ceq
IL_0079: stloc.s CS$4$0001
IL_007b: ldloc.s CS$4$0001
IL_007d: brtrue.s IL_0086
IL_007f: ldloc.3
IL_0080: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0085: nop
IL_0086: endfinally
} // end handler
IL_0087: nop
IL_0088: ret
} // end of method Program::Main
代码转换为try-finally 块,首先它在源上调用GetEnumerator
方法(这是从Where)
返回的迭代器,然后进入try块。
第一个提示:
IL_0055: br.s IL_0067
跳转到IL_0067
以在迭代器上调用MoveNext
,然后将MoveNext
的结果加载到局部变量中(正如奇怪的名称所暗示的那样(CS$4$0001)这是一个编译器生成的变量):
IL_006d: stloc.s CS$4$0001
IL_006f: ldloc.s CS$4$0001
此指令检查从MoveNext
返回的结果是否true
,以及是否跳回到IL_0057
IL_0071: brtrue.s IL_0057
然后继续执行,相同的操作继续运行,直到MoveNext
返回false
。所以是的,代码中有一个循环。
您可以在文档中找到有关IL
说明的更多信息。
除此之外,块之前的代码可能看起来try
混乱,但它基本上创建了一个Func<int, bool>
委托,它是你的lambda表达式(x => x != 2
),然后将其传递给Where
方法。并将其结果加载到 3.(实际上是第四个,3
是索引)这一行中的局部变量:
IL_0054: stloc.3
正如您在参数列表中看到的那样,这是一个IEnumerator<int>
。然后,您的循环使用该迭代器。
单一循环。Where
方法不会首先对所有项执行,它将在枚举项时筛选项。
Where
方法不会生成枚举的集合,而是创建一个枚举器,该枚举器将在枚举项时测试项的条件。这些项目的处理方式与:
foreach (var i in ints) {
if (i != 2) {
Console.WriteLine(i);
}
}
该代码包含用于创建列表、使用枚举器循环的速记代码以及一堆其他内容,因此很难看出它与 IL 代码的关系。这大致是展开速记代码时代码的样子:
Func<int, bool> cachedDelegate;
void Main(string[] args) {
List<int> temp;
int i;
List<int> ints;
IEnumerator<int> enumerator;
temp = new List<int>();
temp.Add(1);
temp.Add(2);
temp.Add(3);
temp.Add(4);
ints = temp;
if (cachedDelegate == null) {
cachedDelegate = new Func<int, bool>(Check);
}
enumerator = ints.Where(cachedDelegate).GetEnumerator();
try {
while (enumerator.MoveNext()) {
i = enumerator.Current;
Console.WriteLine(i);
}
} finally {
if (enumerator != null) {
enumerator.Dispose();
}
}
}
bool Check(int x) {
return x != 2;
}