当前位置:网站首页>C# 迭代器
C# 迭代器
2022-06-09 05:59:00 【Go_Accepted】
1、迭代器
迭代器是程序设计的软件设计模式,提供了一个方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的标识
从表现效果上来看,是可以在容器对象(例如链表或数组)上遍历访问的接口,设计人员无需关心容器对象的内存分配的实现细节,可以用foreach遍历的类,都是实现了迭代器
2、标准迭代器的实现方法
命名空间:using System.Collections;
关键接口:IEnumerable,IEnumerator
可以通过同时继承IEnumerable和IEnumerator实现其中的方法
class CustomList : IEnumerable, IEnumerator { private int[] list; // 从-1开始的光标用于表示数据得到了哪个位置 private int position = -1; public CustomList() { list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; } #region IEnumerable public IEnumerator GetEnumerator() { Reset(); return this; } #endregion #region IEnumerator public object Current { get { return list[position]; } } public bool MoveNext() { // 移动光标判断是否是否溢出,溢出则不合法 return ++position < list.Length; } // reset是重置光标位置,一般写在获取 IEnumerator 对象的函数中 // 用于第一次重置光标位置 public void Reset() { position = -1; } #endregion }CustomList list = new CustomList(); // foreach执行过程: // 1、先获取 in 后面这个对象的 IEnumerator // 会调用对象其中的 GetEnumerator 方法来获取 // (只要有这个方法就可以,即使没有继承IEnumerable接口) // 2、执行得到的这个 IEnumerator 对象中的 MoveNext 方法 // 3、只要这个 MoveNext 方法的返回值是true,就会去得到 Current // 然后赋值给 item foreach (int item in list) { Console.WriteLine(item); }
3、用yield return语法糖实现迭代器
yield return 是C#提供的语法糖
语法糖也就是糖衣语法,主要作用就是将复杂逻辑简单化,可以增加程序的可读性,从而减少程序代码出错的机会
关键接口:IEnumerable
让想通过 foreach 遍历的自定义类实现接口中的方法 GetEnumerator 即可
class CustomList2 : IEnumerable { private int[] list; public CustomList2() { list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; } public IEnumerator GetEnumerator() { for(int i = 0; i < list.Length; ++i) { // yield 关键字配合迭代器使用 // 可以理解为暂时返回,保持当前的状态 yield return list[i]; } } }
4、用yield return语法糖为泛型实现迭代器
class CustomList3<T> : IEnumerable { private T[] list; public CustomList3(params T[] list) { this.list = list; } public IEnumerator GetEnumerator() { for(int i = 0; i < list.Length; ++i) { yield return list[i]; } } }
边栏推荐
- XML modeling
- On input function of istream input stream object CIN
- 【论文】CBAM: Convolutional Block Attention Module
- 代码签名证书的时间戳验证码签名方法
- Window closing process of duilib kernel principle analysis
- Time stamp verification code signing method for code signing certificate
- Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) K - Send the Fool Further! (medi
- pytorch with Automatic Mixed Precision(AMP)
- 对多旅行商问题:应用、方法和分类进行了全面的综述
- Yolov5-6.0 series | yolov5 model network construction
猜你喜欢

MySql使用模糊查询中文时查询语句出现乱码的解决

SET DECIMAL_ V2=false and UDF error: cannot divide decimal by zero and incompatible return types decimal
![Tricks | [trick6] learning rate adjustment strategy of yolov5 (one cycle policy, cosine annealing, etc.)](/img/e8/d11adffb784b0875c69f6fe5a32b63.png)
Tricks | [trick6] learning rate adjustment strategy of yolov5 (one cycle policy, cosine annealing, etc.)

Wireshark图解TCP三次握手与四次挥手

Enter two positive integers m and N to find their maximum common divisor and minimum common multiple.

threadlocal 解析

Detailed understanding and learning of transactions in MySQL (transaction management, transaction isolation level, transaction propagation mechanism)

Topic26——11. Container with the most water

What information does the SSL certificate contain?

Tensorflow introductory tutorial 02 basic concepts of tensorflow (data flow graph, tensor, tensorboard)
随机推荐
vscode 出现TypeScript intellisense is disabled on template.错误
Axure cracking code
fatal: repository not found
Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) C. Jon Snow and his Favou
duilib内核原理分析 之 窗口 关闭流程
New and old versions of Minio tool classes and deployment documents
1433: [example 1] angry cattle
SSL证书包含了哪些信息?
cbnet环境配置和运行中遇到的问题
Offline data synchronization platform datax+ report visualization platform metabase
Build tool gradle
Enter two positive integers m and N to find their maximum common divisor and minimum common multiple.
ThreadLocal parsing
Jdbc-dbutils
ORACLE锁表解决办法
Educational Codeforces Round 20 D. Magazine Ad
Topic25——4. 寻找两个正序数组的中位数
CEF intercepts URLs and redirects new URLs
Educational Codeforces Round 20 E. Roma and Poker
使用 __proto__ 来分配原型