编写高质量代码改善C#程序的157个建议:第17个建议之多数情况下
????? 今天是我看《编写高质量代码:改善C#程序的157个建议》第二遍的时候了,看完这本书的确是受益匪浅,学到了很多东西,也明白了很多道理。 ???? 里面的代码我每个都调试了一遍,有时候是有些出入的,可能是作者写的书比较早,使用的开发环境比较旧,也许是我的学习还不到家,今天在看建议17的时候,发现了一些小问题,不是很大,是小问题,记录下来,当别人看到的时候可以起到修正的作用。 ???? 可能很多人和我一样,没有太在乎for和foreach的区别,也没有深究其底层发生的一些东西,看了文章才发现里面的东西还真是不少。 ??? 好了,我们从头聊一下,我对foreach的认识。 ???? Gof的23种设计模式,我相信大家都看过,我现在也正在写有关《设计模式》的一些文章。在这些设计模式种,有一个设计模式叫“迭代器”,这种设计模式就是针对集合对象的迭代而产生的。具体的模式我就不介绍了,FCL框架中也有针对的此模式的具体实现,具体的接口分别是IEnumerable和IEnumerator接口。迭代器模式屏蔽了各种集合的内部实现,为客户对集合的迭代生成了一个统一接口。foreach的使用语法就是针对FCL框架中实现的迭代器的统一操作实现,简化了语法,方便了客户的实现。 ??? 通过该文章和对IL代码的分析,foreach除了可以提供简化语法外,还有另外两个有事: ????? 1、自动将代码置入try-finally块。 ????? 2、若类型实现了IDisposable接口,他会在循环结束后自动调用Dispose方法。 ?? 这是书里的原话,但是这两大优点是有出入的,并不是所有的集合类型都会将代码自动放入try-finally块中。 ??? 我们先来了解一下集合概况吧,集合类型主要分为两种,一种是线性集合,另一种是非线性集合,如图: ????? ? 1、我们先针对线性集合测试,看看结果怎么样: ????? 1)、线性集合里面的直接存取类型的代表:数组 ????? int[] arra = new int[] { 1,2,1)">3,1)">4,1)">5,1)">6,1)">7 }; foreach (int item in arra) { Console.WriteLine(item); } ? 代码运行结果是: 我们再来看看他们所生成的IL代码: .method private hidebysig static void Main(string[] args) cil managed { .entrypoint // 代码大小 56 (0x38) .maxstack 3 .locals init ([0] int32[] arra,[1] int32[] V_1,1)">2] int32 V_2,1)">] int32 item) IL_0000: nop IL_0001: ldc.i4. IL_0002: newarr [mscorlib]System.Int32 IL_0007: dup IL_0008: ldtoken field valuetype '<PrivateImplementationDetails>'/__StaticArrayInitTypeSize=28' '::447E020739FB3351B9350DB35F297A3AD27669A4' IL_000d: call void [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array,valuetype [mscorlib]System.RuntimeFieldHandle) IL_0012: stloc. IL_0013: nop IL_0014: ldloc. IL_0015: stloc. IL_0016: ldc.i4. IL_0017: stloc. IL_0018: br.s IL_002b IL_001a: ldloc. IL_001b: ldloc. IL_001c: ldelem.i4 IL_001d: stloc. IL_001e: nop IL_001f: ldloc. IL_0020: call void [mscorlib]System.Console::WriteLine(int32) IL_0025: nop IL_0026: nop IL_0027: ldloc. IL_0028: ldc.i4. IL_0029: add IL_002a: stloc. IL_002b: ldloc. IL_002c: ldloc. IL_002d: ldlen IL_002e: conv.i4 IL_002f: blt.s IL_001a IL_0031: call int32 [mscorlib]System.Console::Read() IL_0036: pop IL_0037: ret } end of method Program::Main ?? 我们针对数组执行foreach迭代,但是在所生成的IL代码中并没有看到try-finally中,最起码连try和finally这个两个字样都没看到,书中说foreach遍历集合会自动生成try-finally块,这是不准确的,最起码数组没有生成try-finally代码块。 ??? 2)、然后我们再测试一下string类型,看看他的运行结果怎么样! ?????? 代码如下: string dd = "我是中国人"; var item dd) { Console.WriteLine(item); } ?执行效果如图: 让我们再来看看它所生成的IL代码吧,不要惊讶: .method 代码大小 51 (0x33) .maxstack 0] dd,1)">1] V_1,1)">3] char item) IL_0000: nop IL_0001: ldstr bytearray (11 62 2F 66 2D 4E FD 56 BA 4E ) .b/f-N.V.N IL_0006: stloc. IL_0007: nop IL_0008: ldloc. IL_0009: stloc. IL_000a: ldc.i4. IL_000b: stloc. IL_000c: br.s IL_0023 IL_000e: ldloc. IL_000f: ldloc. IL_0010: callvirt instance [mscorlib]System.String::get_Chars(int32) IL_0015: stloc. IL_0016: nop IL_0017: ldloc. IL_0018: call void [mscorlib]System.Console::WriteLine() IL_001d: nop IL_001e: nop IL_001f: ldloc. IL_0020: ldc.i4. IL_0021: add IL_0022: stloc. IL_0023: ldloc. IL_0024: ldloc. IL_0025: callvirt instance int32 [mscorlib]System.String::get_Length() IL_002a: blt.s IL_000e IL_002c: call int32 [mscorlib]System.Console::Read() IL_0031: pop IL_0032: ret } end of method Program::Main ? 这里面也没有生成try-finally代码块,说明并不是所有的集合都会自动生成try-finally代码块的。 3)、我们继续测试一下Dictionary<TKey,TValue>,List<T>,Queue<T>,Stack<T>,ArrayList类型: ???? (1)、Dictionary<TKey,TValue>测试代码: Dictionary<string,string> dictionary = new Dictionary<string>(); dictionary.Add(1",1)">11); dictionary.Add(222333444555666777); dictionary) { Console.WriteLine(item.Key + ----" + item.Value); } ????? 字典类型所生成IL代码如下: .method 代码大小 209 (0xd1) .maxstack class [mscorlib]System.Collections.Generic.Dictionary`2< dictionary,1)">1] valuetype [mscorlib]System.Collections.Generic.Dictionary`2/Enumerator<2] valuetype [mscorlib]System.Collections.Generic.KeyValuePair` item) IL_0000: nop IL_0001: newobj instance void ::.ctor() IL_0006: stloc. IL_0007: ldloc. IL_0008: ldstr IL_000d: ldstr IL_0012: callvirt instance string>::Add(!,!) IL_0017: nop IL_0018: ldloc. IL_0019: ldstr IL_001e: ldstr IL_0023: callvirt instance ) IL_0028: nop IL_0029: ldloc. IL_002a: ldstr IL_002f: ldstr IL_0034: callvirt instance ) IL_0039: nop IL_003a: ldloc. IL_003b: ldstr IL_0040: ldstr IL_0045: callvirt instance ) IL_004a: nop IL_004b: ldloc. IL_004c: ldstr IL_0051: ldstr IL_0056: callvirt instance ) IL_005b: nop IL_005c: ldloc. IL_005d: ldstr IL_0062: ldstr IL_0067: callvirt instance ) IL_006c: nop IL_006d: ldloc. IL_006e: ldstr IL_0073: ldstr IL_0078: callvirt instance ) IL_007d: nop IL_007e: nop IL_007f: ldloc. IL_0080: callvirt instance valuetype [mscorlib]System.Collections.Generic.Dictionary`2/Enumerator<!0,!1> ::GetEnumerator() IL_0085: stloc. .try { IL_0086: br.s IL_00b0 IL_0088: ldloca.s V_1 IL_008a: call instance valuetype [mscorlib]System.Collections.Generic.KeyValuePair`2<!1> valuetype [mscorlib]System.Collections.Generic.Dictionary`::get_Current() IL_008f: stloc. IL_0090: nop IL_0091: ldloca.s item IL_0093: call instance !0 valuetype [mscorlib]System.Collections.Generic.KeyValuePair`::get_Key() IL_0098: ldstr IL_009d: ldloca.s item IL_009f: call instance !1 valuetype [mscorlib]System.Collections.Generic.KeyValuePair`::get_Value() IL_00a4: call string [mscorlib]System.String::Concat() IL_00a9: call ) IL_00ae: nop IL_00af: nop IL_00b0: ldloca.s V_1 IL_00b2: call instance bool valuetype [mscorlib]System.Collections.Generic.Dictionary`::MoveNext() IL_00b7: brtrue.s IL_0088 IL_00b9: leave.s IL_00ca } end .try finally { IL_00bb: ldloca.s V_1 IL_00bd: constrained. valuetype [mscorlib]System.Collections.Generic.Dictionary` IL_00c3: callvirt instance [mscorlib]System.IDisposable::Dispose() IL_00c8: nop IL_00c9: endfinally } end handler IL_00ca: call int32 [mscorlib]System.Console::Read() IL_00cf: pop IL_00d0: ret } end of method Program::Main ? 哈哈哈,终于出现了,自动生成try-finally代码块,并且调用了Dispose方法。 (2)List<T>,Queue<T>,Stack<T>,ArrayList测试代码如下: ???? List<int> list = new List<int>() { }; list) { Console.WriteLine(item); } .method 代码大小 83 (0x53) .maxstack class [mscorlib]System.Collections.Generic.List`1<int32> list,1)">1] valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<int32>] int32 item) IL_0000: nop IL_0001: newobj instance ::.ctor() IL_0006: dup IL_0007: ldc.i4. IL_0008: callvirt instance 1<int32>::Add(!) IL_000d: nop IL_000e: dup IL_000f: ldc.i4.) IL_0015: nop IL_0016: stloc. IL_0017: nop IL_0018: ldloc. IL_0019: callvirt instance valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<!0> ::GetEnumerator() IL_001e: stloc. { IL_001f: br.s IL_0032 IL_0021: ldloca.s V_1 IL_0023: call instance !0 valuetype [mscorlib]System.Collections.Generic.List`::get_Current() IL_0028: stloc. IL_0029: nop IL_002a: ldloc. IL_002b: call [mscorlib]System.Console::WriteLine(int32) IL_0030: nop IL_0031: nop IL_0032: ldloca.s V_1 IL_0034: call instance bool valuetype [mscorlib]System.Collections.Generic.List`::MoveNext() IL_0039: brtrue.s IL_0021 IL_003b: leave.s IL_004c } { IL_003d: ldloca.s V_1 IL_003f: constrained. valuetype [mscorlib]System.Collections.Generic.List` IL_0045: callvirt instance [mscorlib]System.IDisposable::Dispose() IL_004a: nop IL_004b: endfinally } IL_004c: call int32 [mscorlib]System.Console::Read() IL_0051: pop IL_0052: ret } end of method Program::Main 其他的代码我就不贴了,都生成了try-finally代码块,如果类型实现了IDisposable接口,也会调用Dispose方法。 2、我们再测试一下非线性集合是否会自动产生相关代码 ??? 我们测试一下HashSet<T>代码,看看效果怎么样。 ??? HashSet<string> hash = new HashSet<(); hash.Add(); hash.Add(); hash) { Console.WriteLine(item); } ?? 运行效果图: ?? ?最后我们看看IL代码: ?? .method 代码大小 139 (0x8b) .maxstack class [System.Core]System.Collections.Generic.HashSet`1< hash,1)">1] valuetype [System.Core]System.Collections.Generic.HashSet`1/Enumerator<2] IL_000d: callvirt instance bool ) IL_0012: pop IL_0013: ldloc. IL_0014: ldstr IL_0019: callvirt instance ) IL_001e: pop IL_001f: ldloc. IL_0020: ldstr IL_0025: callvirt instance ) IL_002a: pop IL_002b: ldloc. IL_002c: ldstr IL_0031: callvirt instance ) IL_0036: pop IL_0037: ldloc. IL_0038: ldstr IL_003d: callvirt instance ) IL_0042: pop IL_0043: ldloc. IL_0044: ldstr IL_0049: callvirt instance ) IL_004e: pop IL_004f: nop IL_0050: ldloc. IL_0051: callvirt instance valuetype [System.Core]System.Collections.Generic.HashSet`::GetEnumerator() IL_0056: stloc. { IL_0057: br.s IL_006a IL_0059: ldloca.s V_1 IL_005b: call instance !0 valuetype [System.Core]System.Collections.Generic.HashSet`::get_Current() IL_0060: stloc. IL_0061: nop IL_0062: ldloc. IL_0063: call ) IL_0068: nop IL_0069: nop IL_006a: ldloca.s V_1 IL_006c: call instance bool valuetype [System.Core]System.Collections.Generic.HashSet`::MoveNext() IL_0071: brtrue.s IL_0059 IL_0073: leave.s IL_0084 } { IL_0075: ldloca.s V_1 IL_0077: constrained. valuetype [System.Core]System.Collections.Generic.HashSet` IL_007d: callvirt instance [mscorlib]System.IDisposable::Dispose() IL_0082: nop IL_0083: endfinally } IL_0084: call int32 [mscorlib]System.Console::Read() IL_0089: pop IL_008a: ret } end of method Program::Main ? 好了,总结一下吧,如果不涉及到修改集合元素,一般情况下使用Foreach比较好,在我测试的代码中,数组和String数据类型不会生成try-finally代码块,里面有一些出入而已,但是这并不影响Foreach的使用。所以我们在具体的编码过程中,尽量多的使用Foreach吧,没关系。 ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |