自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#迭代器局部變量

開發(fā) 后端
這里介紹C#迭代器局部變量,IEnumerator就是C#迭代器的接口,相當(dāng)于我的實(shí)例里面的Iterator,它也有泛型的版本。

C#迭代器還是比較常見的東西,這里我們主要介紹C#迭代器局部變量,包括介紹C#里出現(xiàn)了foreach關(guān)鍵字等方面。

看看***的測試,是不是不管具體的集合如何改變,遍歷代碼都非常穩(wěn)定?而且擴(kuò)展新的集合類也非常方便,只是添加代碼不會修改原來的代碼,符合開閉原則。當(dāng)然,這么好的解決方案微軟當(dāng)然不會放過,現(xiàn)在C# 2.0里已經(jīng)內(nèi)置了對C#迭代器的支持,看看System.Collections, System.Collections.Generic命名空間,所有的集合都實(shí)現(xiàn)了這個(gè)接口:IEnumerable,這個(gè)接口還有泛型的版本。注意到這個(gè)接口只有一個(gè)方法:IEnumerator GetEnumerator();,IEnumerator就是C#迭代器的接口,相當(dāng)于我的實(shí)例里面的Iterator,它也有泛型的版本。

那么現(xiàn)在在.net里所有的集合類都可以這樣訪問了:

  1. IEnumerator ienumerator = list.GetEnumerator();  
  2. while(ienumerator.MoveNext())  
  3. {  
  4. object current = ienumerator.Current;  

但是這樣訪問也太麻煩了,所以C#里出現(xiàn)了foreach關(guān)鍵字,我們來看看foreach背后發(fā)生了什么

  1. public static void Main()  
  2. {  
  3. ArrayList list = new ArrayList();  
  4. list.Add(1);  
  5. list.Add(2);  
  6. list.Add(3);  
  7. foreach (object item in list)  
  8. {  
  9. Console.WriteLine(item.ToString());  
  10. }  
  11. }  

下面是它對應(yīng)的IL代碼:

  1. .method private hidebysig static void Main() cil managed  
  2. {  
  3. .entrypoint  
  4. .maxstack 2  
  5. .locals init (  
  6. [0] class [mscorlib]System.Collections.ArrayList list,  
  7. [1] object item,  
  8. [2] class [mscorlib]System.Collections.IEnumerator CS$5$0000,  
  9. [3] class [mscorlib]System.IDisposable CS$0$0001)  
  10. L_0000: newobj instance void [mscorlib]System.Collections.ArrayList::.ctor()  
  11. L_0005: stloc.0   
  12. L_0006: ldloc.0   
  13. L_0007: ldc.i4.1   
  14. L_0008: box int32  
  15. L_000d: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object)  
  16. L_0012: pop   
  17. L_0013: ldloc.0   
  18. L_0014: ldc.i4.2   
  19. L_0015: box int32  
  20. L_001a: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object)  
  21. L_001f: pop   
  22. L_0020: ldloc.0   
  23. L_0021: ldc.i4.3   
  24. L_0022: box int32  
  25. L_0027: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object)  
  26. L_002c: pop   
  27. L_002d: ldloc.0   
  28. L_002e: callvirt instance class [mscorlib]System.Collections.IEnumerator [mscorlib]
  29. System.Collections.ArrayList::GetEnumerator()  
  30. L_0033: stloc.2   
  31. L_0034: br.s L_0048  
  32. L_0036: ldloc.2   
  33. L_0037: callvirt instance object [mscorlib]System.Collections.IEnumerator::get_Current()  
  34. L_003c: stloc.1   
  35. L_003d: ldloc.1   
  36. L_003e: callvirt instance string [mscorlib]System.Object::ToString()  
  37. L_0043: call void [mscorlib]System.Console::WriteLine(string)  
  38. L_0048: ldloc.2   
  39. L_0049: callvirt instance bool [mscorlib]System.Collections.IEnumerator::MoveNext()  
  40. L_004e: brtrue.s L_0036  
  41. L_0050: leave.s L_0063  
  42. L_0052: ldloc.2   
  43. L_0053: isinst [mscorlib]System.IDisposable  
  44. L_0058: stloc.3   
  45. L_0059: ldloc.3   
  46. L_005a: brfalse.s L_0062  
  47. L_005c: ldloc.3   
  48. L_005d: callvirt instance void [mscorlib]System.IDisposable::Dispose()  
  49. L_0062: endfinally   
  50. L_0063: call string [mscorlib]System.Console::ReadLine()  
  51. L_0068: pop   
  52. L_0069: ret   
  53. .try L_0034 to L_0052 finally handler L_0052 to L_0063  

從.locals init 那里可以看出編譯器為我們添加了兩個(gè)C#迭代器局部變量,一個(gè)就是C#迭代器。

  1. L_002d: ldloc.0   
  2. L_002e: callvirt instance class [mscorlib]System.Collections.IEnumerator [mscorlib]
  3. System.Collections.ArrayList::GetEnumerator()  
  4. L_0033: stloc.2  

這三行代碼告訴我們,調(diào)用list的GetEnumerator()方法,獲取C#迭代器實(shí)例將其賦值給編譯器為我們添加的那個(gè)C#迭代器局部變量,接著是L_0034: br.s L_0048,br.s這個(gè)指令是強(qiáng)制跳轉(zhuǎn),我們接著看

  1. L_0048: ldloc.2   
  2. L_0049: callvirt instance bool [mscorlib]System.Collections.IEnumerator::MoveNext() 

【編輯推薦】

  1. C# lock關(guān)鍵字?jǐn)⑹?/FONT>
  2. C#.Net FrameWork簡介
  3. C# new和override簡單描述
  4. C#值類型和引用類型淺談
  5. C#標(biāo)識符簡單分析
責(zé)任編輯:佚名 來源: IT168
相關(guān)推薦

2009-08-27 10:08:36

C#隱含類型局部變量

2009-08-26 15:39:08

C#隱式類型局部變量

2009-09-17 13:05:38

Linq局部變量類型

2009-08-11 13:59:41

迭代器模式C# Iterator

2009-08-26 16:26:37

C#迭代器模式

2009-08-19 15:18:53

迭代器

2018-05-14 09:15:24

Python變量函數(shù)

2009-10-12 14:13:00

VB.NET使用局部變

2009-09-11 10:07:05

Linq隱式類型化局部

2009-12-15 10:48:54

Ruby局部變量

2020-11-11 21:26:48

函數(shù)變量

2024-05-29 08:49:22

Python全局變量局部變量

2015-01-07 14:41:32

Android全局變量局部變量

2021-09-28 07:12:09

函數(shù)內(nèi)存

2010-03-15 09:32:56

Python函數(shù)

2017-02-08 12:28:37

Android變量總結(jié)

2009-09-22 17:21:24

線程局部變量

2023-09-24 23:40:54

Python變量

2009-08-18 17:17:05

C#局部類型

2010-10-14 09:34:34

JVM局部變量
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號