網站首頁 編程語言 正文
1.迭代器方法
可以使用foreach
循環語句進行的迭代的方法,稱為可迭代方法,或者迭代器方法。
迭代器用法法介紹。
迭代器用于依次返回每個元素,一般用于foreach循環語句。迭代器方法需要使用yield return語句。
?yield return 語句介紹:
保持代碼的當前位置,在下一次調用迭代器方法時執行。
?迭代方法在使用過程中左右步驟對應。yield return語句主要是返回一個結果作為函數調用的結果。并記錄當前運行位置,當下次函數被調用時,在當前位置執行這個函數。在迭代塊中除了yield return
外,不允許出現普通的return語句。
迭代方法使用的命名空間為using System.Collections.Generic
;
下面代碼為迭代器使用的具體代碼:
class Program { ? ? public static IEnumerableFibs() ? ? { ? ? ? ? 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關系到你迭代對象yield return返回值的類型。如果你定義IEnumerableIEnumerable
那么你的返回值是string類型以此類推。如果你想以某個條件結束方法。可以使用外面的條件如上圖所示。也可以使用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.手動實現迭代器方法
首先是通過使用接口IEnumerable
的方式,然后編寫IEnumerator GetEnumerator()的方式。在代碼中控制索引位置,和循環次數。如果索引位置出錯則使用代碼throw new NotImplementedException()
報錯。
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; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? } ? ? } }
原文鏈接:https://www.cnblogs.com/guiyan/p/15970008.html
相關推薦
- 2022-05-26 .Net解決引用程序集沒有強名稱報錯_實用技巧
- 2022-04-24 淺析GBase8s?唯一索引與非唯一索引問題_數據庫其它
- 2022-09-25 linux基礎入門1 vi/vim編輯器
- 2022-05-02 C/C++的各種字符串函數你知道幾個_C 語言
- 2022-08-07 Android?AccessibilityService?事件分發原理分析總結_Android
- 2022-05-12 Kotlin set集合去重,獲取元素可變set集合,set與list轉換
- 2022-12-12 Android?WindowManager深層理解view繪制實現流程_Android
- 2023-02-15 Python函數常見幾種return返回值類型_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支