为什么在GetCurrent之前调用IEnumerator.MoveNext?

时间:2015-08-21 17:59:33

标签: c# .net foreach il

我对IL完全不熟悉,但据我所知MoveNext应该在Current之前调用,让我们说我们有这样的foreach声明:

foreach (var i in Enumerable.Empty<string>())
{          
}

如果我们查看生成的IL,我们将看到Current实际上是第一次调用:

IL_0014:  br.s       IL_001f
IL_0016:  ldloc.1
IL_0017:  callvirt   instance !0 class [mscorlib]System.Collections.Generic.IEnumerator`1<string>::get_Current()
IL_001c:  stloc.0
IL_001d:  nop
IL_001e:  nop
IL_001f:  ldloc.1
IL_0020:  callvirt   instance bool [mscorlib]System.Collections.IEnumerator::MoveNext()
IL_0025:  stloc.2
IL_0026:  ldloc.2
IL_0027:  brtrue.s   IL_0016
IL_0029:  leave.s    IL_003b

问题是为什么?

1 个答案:

答案 0 :(得分:4)

IL_0014:  br.s       IL_001f

根据MSDN,br.s

  

无条件地将控制转移到目标指令(简短形式)。

引用的地址是调用MoveNext

的指令
IL_001f:  ldloc.1
IL_0020:  callvirt   instance bool [mscorlib]System.Collections.IEnumerator::MoveNext()