C#特性-迭代器(上)及一些研究过程中的副产品

2019-12-26 11:17:57刘景俊
  •   L_001f: pop     L_0020: ldloc.0  
  •   L_0021: ldc.i4.3     L_0022: box int32 
  •   L_0027: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object)    L_002c: pop  
  •   L_002d: ldloc.0     L_002e: callvirt instance class [mscorlib]System.Collections.IEnumerator [mscorlib]System.Collections.ArrayList::GetEnumerator() 
  •   L_0033: stloc.2     L_0034: br.s L_0048 
  •   L_0036: ldloc.2     L_0037: callvirt instance object [mscorlib]System.Collections.IEnumerator::get_Current() 
  •   L_003c: stloc.1     L_003d: ldloc.1  
  •   L_003e: callvirt instance string [mscorlib]System.Object::ToString()    L_0043: call void [mscorlib]System.Console::WriteLine(string) 
  •   L_0048: ldloc.2     L_0049: callvirt instance bool [mscorlib]System.Collections.IEnumerator::MoveNext() 
  •   L_004e: brtrue.s L_0036    L_0050: leave.s L_0063 
  •   L_0052: ldloc.2     L_0053: isinst [mscorlib]System.IDisposable 
  •   L_0058: stloc.3     L_0059: ldloc.3  
  •   L_005a: brfalse.s L_0062    L_005c: ldloc.3  
  •   L_005d: callvirt instance void [mscorlib]System.IDisposable::Dispose()    L_0062: endfinally  
  •   L_0063: call string [mscorlib]System.Console::ReadLine()    L_0068: pop  
  •   L_0069: ret     .try L_0034 to L_0052 finally handler L_0052 to L_0063 
  • }  ?

    从.locals init 那里可以看出编译器为我们添加了两个局部变量,一个就是迭代器。

     

    复制代码
    L_002d: ldloc.0 
    L_002e: callvirt instance class [mscorlib]System.Collections.IEnumerator [mscorlib]System.Collections.ArrayList::GetEnumerator()