你們好,這是 [C#.NET 拾遺補漏] 系列的第 07 篇文章。數組
在 C# 中,大多數方法都是經過 return 語句當即把程序的控制權交回給調用者,同時也會把方法內的本地資源釋放掉。而包含 yield 語句的方法則容許在依次返回多個值給調用者的期間保留本地資源,等全部值都返回結束時再釋放掉原本資源,這些返回的值造成一組序列被調用者使用。在 C# 中,這種包含 yield 語句的方法、屬性或索引器就是迭代器。bash
迭代器中的 yield 語句分爲兩種:app
yeild return
,把程序控制權交回調用者並保留本地狀態,調用者拿到返回的值繼續日後執行。yeild break
,用於告訴程序當前序列已經結束,至關於正常代碼塊的 return 語句(迭代器中直接使用 return 是非法的)。下面是一個用來生成斐波納契序列的迭代器示例:ui
IEnumerable<int> Fibonacci(int count) { int prev = 1; int curr = 1; for (int i = 0; i < count; i++) { yield return prev; int temp = prev + curr; prev = curr; curr = temp; } } void Main() { foreach (int term in Fibonacci(10)) { Console.WriteLine(term); } }
輸出:code
1 1 2 3 5 8 13 21 34 55
實際場景中,咱們通常不多直接寫迭代器,由於大部分須要迭代的場景都是數組、集合和列表,而這些類型內部已經封裝好了所需的迭代器。好比 C# 中的數組之因此能夠被遍歷是由於它實現了 IEnumerable
接口,經過 GetEnumerator()
方法能夠得到數組的列舉器 Enumerator,而該列舉器就是經過迭代器來實現的。好比最多見的一種使用場景就是遍歷數組中的每個元素,以下面逐個打印數組元素的示例。對象
int[] numbers = { 1, 2, 3, 4, 5 }; IEnumerator enumerator = numbers.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current); }
其實這就是 foreach 的工做原理,上面代碼能夠用 foreach 改寫以下:索引
int[] numbers = { 1, 2, 3, 4, 5 }; foreach (int number in numbers) { Console.WriteLine(number); }
固然,列舉器不必定非要經過迭代器實現,例以下面這個自定義的列舉器 CoffeeEnumerator。接口
public class CoffeeCollection : IEnumerable { private CoffeeEnumerator enumerator; public CoffeeCollection() { enumerator = new CoffeeEnumerator(); } public IEnumerator GetEnumerator() { return enumerator; } public class CoffeeEnumerator : IEnumerator { string[] items = new string[3] { "espresso", "macchiato", "latte" }; int currentIndex = -1; public object Current { get { return items[currentIndex]; } } public bool MoveNext() { currentIndex++; if (currentIndex < items.Length) { return true; } return false; } public void Reset() { currentIndex = 0; } } }
使用:ci
public static void Main(string[] args) { foreach (var coffee in new CoffeeCollection()) { Console.WriteLine(coffee); } }
理解迭代器和列舉器能夠幫助咱們寫出更高效的代碼。好比判斷一個 IEnumerable<T>
對象是否包含元素,常常看到有些人這麼寫:資源
if(enumerable.Count() > 0) { // 集合中有元素 }
但若是用列舉器的思惟稍微思考一下就知道,Count()
爲了得到集合元素數量必然要迭代完全部元素,時間複雜度爲 O(n)。而僅僅是要知道集合中是否包含元素,其實迭代一次就能夠了。因此效率更好的作法是:
if(enumerable.GetEnumerator().MoveNext()) { // 集合中有元素 }
這樣寫時間複雜度是 O(1),效率顯然更高。爲了書寫方便,C# 提供了擴展方法 Any()
。
if(enumerable.Any()) { // 集合中有元素 }
因此若有須要,應儘量使用 Any 方法,效率更高。
再好比在 EF Core 中,須要執行 IQueryable<T>
查詢時,有時候使用 AsEnumerable()
比使用 ToList、ToArray 等更高效,由於 ToList、ToArray 等會當即執行列舉操做,而 AsEnumerable()
能夠把列舉操做延遲到真正被須要的時候再執行。固然也要考慮實際應用場景,Array、List 等更方便調用者使用,特別是要獲取元素總數量、增刪元素等這種操做。