C#迭代器局部变量
C#迭代器还是比较常见的东西,这里我们主要介绍C#迭代器局部变量,包括介绍C#里出现了foreach关键字等方面。
看看最后的测试,是不是不管具体的集合如何改变,遍历代码都非常稳定?而且扩展新的集合类也非常方便,只是添加代码不会修改原来的代码,符合开闭原则。当然,这么好的解决方案微软当然不会放过,现在C# 2.0里已经内置了对C#迭代器的支持,看看System.Collections, System.Collections.Generic命名空间,所有的集合都实现了这个接口:IEnumerable,这个接口还有泛型的版本。注意到这个接口只有一个方法:IEnumerator GetEnumerator();,IEnumerator就是C#迭代器的接口,相当于我的实例里面的Iterator,它也有泛型的版本。
那么现在在.net里所有的集合类都可以这样访问了:
IEnumerator ienumerator = list.GetEnumerator(); while(ienumerator.MoveNext()) { object current = ienumerator.Current; }
但是这样访问也太麻烦了,所以C#里出现了foreach关键字,我们来看看foreach背后发生了什么
public static void Main() { ArrayList list = new ArrayList(); list.Add(1); list.Add(2); list.Add(3); foreach (object item in list) { Console.WriteLine(item.ToString()); } }
下面是它对应的IL代码:
.method private hidebysig static void Main() cil managed { .entrypoint .maxstack 2 .locals init ( [0] class [mscorlib]System.Collections.ArrayList list, [1] object item, [2] class [mscorlib]System.Collections.IEnumerator CS$5$0000, [3] class [mscorlib]System.IDisposable CS$0$0001) L_0000: newobj instance void [mscorlib]System.Collections.ArrayList::.ctor() L_0005: stloc.0 L_0006: ldloc.0 L_0007: ldc.i4.1 L_0008: box int32 L_000d: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object) L_0012: pop L_0013: ldloc.0 L_0014: ldc.i4.2 L_0015: box int32 L_001a: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object) 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 那里可以看出编译器为我们添加了两个C#迭代器局部变量,一个就是C#迭代器。
L_002d: ldloc.0 L_002e: callvirt instance class [mscorlib]System.Collections.IEnumerator [mscorlib] System.Collections.ArrayList::GetEnumerator() L_0033: stloc.2
相关推荐
JnX 2020-09-21
joyjoy0 2020-09-18
Jan 2020-08-17
shenxiuwen 2020-08-01
Andrewjdw 2020-07-26
fanhuasijin 2020-06-21
丽丽 2020-06-08
容数据服务集结号 2020-06-08
czsay 2020-06-01
程序员之怒 2020-04-26
wmsjlihuan 2020-04-26
oDongTianShuiYue 2020-04-26
kong000dao0 2020-04-29
breakpoints 2020-04-20
卷卷萌 2020-04-20
RocNg 2020-04-18
小菜鸟的代码世界 2020-03-27
HongKongPython 2020-03-26