C#迭代器方法介紹
1.迭代器方法
可以使用foreach
循環(huán)語句進(jìn)行的迭代的方法,稱為可迭代方法,或者迭代器方法。
迭代器用法法介紹。
迭代器用于依次返回每個(gè)元素,一般用于foreach循環(huán)語句。迭代器方法需要使用yield return語句。
yield return 語句介紹:
保持代碼的當(dāng)前位置,在下一次調(diào)用迭代器方法時(shí)執(zhí)行。
迭代方法在使用過程中左右步驟對應(yīng)。yield return語句主要是返回一個(gè)結(jié)果作為函數(shù)調(diào)用的結(jié)果。并記錄當(dāng)前運(yùn)行位置,當(dāng)下次函數(shù)被調(diào)用時(shí),在當(dāng)前位置執(zhí)行這個(gè)函數(shù)。在迭代塊中除了yield return
外,不允許出現(xiàn)普通的return語句。
迭代方法使用的命名空間為using System.Collections.Generic
;
下面代碼為迭代器使用的具體代碼:
class Program { ? ? public static IEnumerable<int> Fibs() ? ? { ? ? ? ? int f1 = 1, f2 = 2; ? ? ? ? while (true) ? ? ? ? { ? ? ? ? ? ? yield return f1; ? ? ? ? ? ? yield return f2; ? ? ? ? ? ? f1 += f2; ? ? ? ? ? ? f2 += f1; ? ? ? ? } ? ? } ? ? static void Main(string[] args) ? ? { ? ? ? ? foreach (int i in Fibs()) ? ? ? ? ? ? if (i < 20) ? ? ? ? ? ? ? ? Console.WriteLine("{0}", i); ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? break; ? ? ? ? Console.ReadKey(); ? ? }? }
IEnumerable
是泛型定義的里面的int關(guān)系到你迭代對象yield return返回值的類型。如果你定義IEnumerable<int>那么你返回的值是int類型,如果你定義IEnumerable
那么你的返回值是string類型以此類推。如果你想以某個(gè)條件結(jié)束方法。可以使用外面的條件如上圖所示。也可以使用yield break。
class Program { ? ? public static IEnumerable Fibs() ? ? { ? ? ? ? string f1 = "1", f2 = "2"; ? ? ? ? while (true) ? ? ? ? { ? ? ? ? ? ? yield return f1; ? ? ? ? ? ? yield return f2; ? ? ? ? ? ? f1 += f2; ? ? ? ? ? ? f2 += f1; ? ? ? ? ? ? if (f1.Length > 8) ? ? ? ? ? ? ? ? yield break; ? ? ? ? } ? ? } ? ? ? static void Main(string[] args) ? ? { ? ? ? ? foreach (string i in Fibs()) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("{0}", i); ? ? ? ? Console.ReadKey(); ? ? }? }
2.手動(dòng)實(shí)現(xiàn)迭代器方法
首先是通過使用接口IEnumerable
的方式,然后編寫IEnumerator GetEnumerator()的方式。在代碼中控制索引位置,和循環(huán)次數(shù)。如果索引位置出錯(cuò)則使用代碼throw new NotImplementedException()
報(bào)錯(cuò)。
using System; using System.Collections; using System.Collections.Generic; ? ? namespace test02 { ? ? class Program ? ? { ? ? ? ? static void Main(string[] args) ? ? ? ? { ? ? ? ? ? ? object [] e = new object[5] { 1, 2, 3, 4, 5 }; ? ? ? ? ? ? Itear01 s = new Itear01(e,2); ? ? ? ? ? ? foreach (object i in s) ? ? ? ? ? ? ? ? ? ? Console.WriteLine("{0}", i); ? ? ? ? ? ? Console.ReadKey(); ? ? ? ? }? ? ? } ? ? ? public class Itear01 : IEnumerable ? ? { ? ? ? ? object[] values; ? ? ? ? int StartPoint=-1; ? ? ? ? int current=0; ? ? ? ? public Itear01(object[] values,int StartPoint) ? ? ? ? { ? ? ? ? ? ? this.values = values; ? ? ? ? ? ? this.StartPoint = StartPoint; ? ? ? ? } ? ? ? ? public IEnumerator GetEnumerator() ? ? ? ? { ? ? ? ? ? ? if(this.StartPoint==-1) ? ? ? ? ? ? ? ? throw new NotImplementedException(); ? ? ? ? ? ? while(true) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? yield return this.values[StartPoint]; ? ? ? ? ? ? ? ? StartPoint = (StartPoint + 1) % values.Length; ? ? ? ? ? ? ? ? current++; ? ? ? ? ? ? ? ? if (current == values.Length) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? } ? ? } }
到此這篇關(guān)于C#迭代器方法介紹的文章就介紹到這了,更多相關(guān)C#迭代器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)把txt文本數(shù)據(jù)快速讀取到excel中
這篇文章主要介紹了C#實(shí)現(xiàn)把txt文本數(shù)據(jù)快速讀取到excel中,本文直接給出示例代碼,需要的朋友可以參考下2015-06-06C#檢查Windows是否安裝了某個(gè)服務(wù)的方法
這篇文章主要介紹了C#檢查Windows是否安裝了某個(gè)服務(wù)的方法,涉及C#操作windows系統(tǒng)服務(wù)的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03C#遍歷得到checkboxlist選中值和設(shè)置選中項(xiàng)的代碼
這篇文章主要介紹了C#遍歷得到checkboxlist選中值和設(shè)置選中項(xiàng)的代碼,代碼簡單易懂,具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08