網站首頁 編程語言 正文
1、對象只要一個類型實現了IEnumerable接口就能遍歷
2、IEnumerator是枚舉器,一個接口類,實現MoveNext->Current->Reset
3、yield關鍵字是一個迭代器,相當于實現了IEnumerator枚舉器
4、IEnumerable是可枚舉類型,IEnumerator是枚舉器
public class IEnumerableShow {
? ? ? ? public void Show() {
? ? ? ? ? ? int[] array = { 1, 2, 3, 4, 5 };
? ? ? ? ? ? Student student = new Student { Id = 1 };
? ? ? ? ? ? foreach (var item in array) {
? ? ? ? ? ? ? ? Console.WriteLine();
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? class Student:IEnumerable {?
? ? ? ? public int Id { get; set; }
? ? ? ? public IEnumerator GetEnumerator() { //返回一個枚舉器
? ? ? ? ? ? //yield return "Ant編程1";
? ? ? ? ? ? //yield return "Ant編程2";
? ? ? ? ? ? //yield return "Ant編程3";
? ? ? ? ? ? string[] student = { "Ant編程1", "Ant編程2", "Ant編程3" };
? ? ? ? ? ? return new StudentEnumerator(student);
? ? ? ? }
? ? }
? ? internal class StudentEnumerator : IEnumerator
? ? {
? ? ? ? string[] _student;
? ? ? ? int _position = -1;
? ? ? ? public StudentEnumerator(string[] student) {
? ? ? ? ? ? this._student = student;
? ? ? ? }
? ? ? ? public object Current {
? ? ? ? ? ? get {
? ? ? ? ? ? ? ? if (_position == -1) {
? ? ? ? ? ? ? ? ? ? throw new InvalidOperationException();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (_position>=_student.Length) {
? ? ? ? ? ? ? ? ? ? throw new InvalidOperationException();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return _student[_position];
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? public bool MoveNext()
? ? ? ? {
? ? ? ? ? ? if (_position<_student.Length-1) {
? ? ? ? ? ? ? ? _position++;
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? ? ? else {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? public void Reset()
? ? ? ? {
? ? ? ? ? ? _position = -1;
? ? ? ? }
? ? }
IEnumerator , IEnumerable 枚舉器接口
IEnumerator是枚舉器的意思,IEnumerable是可枚舉的意思。這兩個都是個接口
foreach是一種語法糖,用來簡化對可枚舉元素的遍歷代碼。而被遍歷的類通過實現IEnumerable接口和一個相關的IEnumerator枚舉器來實現遍歷功能。
public class MyList : IEnumerable
{
public int[] _data = new int[10] { 1, 5, 7, 9, 7, 8, 7, 8, 7, 4 };
public int this[int index]
{
get
{
return _data[index];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
Debug.Log("foreach調用 GetEnumerator");
return new MIEnumtor(this);
}
}
public class MIEnumtor : IEnumerator
{
private MyList myList;
private int index;
public MIEnumtor(MyList my)
{
index = -1;
myList = my;
}
public object Current
{
get
{
Debug.Log("foreach調用 Current");
return myList[index];
}
}
public bool MoveNext()
{
Debug.Log("foreach調用 MoveNext");
if (index < myList._data.Length - 1)
{
index++;
return true;
}
index = -1;
return false;
}
public void Reset()
{
}
}
GetIEnumerator()負責獲取枚舉器。
MoveNext()負責讓Current獲取下一個值,并判斷遍歷是否結束。
Current負責返回當前指向的值。
Rest()負責重置枚舉器的狀態(在foreach中沒有用到)
這些就是IEnumerable,IEnumerator的基本工作原理了。
MyList my = new MyList();
foreach (var item in my)
{
Debug.Log(item);
}
等價于
MyList my = new MyList();
MIEnumtor mIEnumtor = my.GetEnumerator();
while (mIEnumtor.MoveNext())
{
Debug.Log(mIEnumtor.Current);
}
原文鏈接:https://blog.csdn.net/HobbitX/article/details/124407904
相關推薦
- 2022-06-06 Rust字符串字面值的一些經驗總結_相關技巧
- 2022-09-25 linux系統下oracle數據庫的導入導出
- 2022-12-04 C++?Boost.Range與Adapters庫使用詳解_C 語言
- 2022-07-07 一篇文章讀懂nginx的gzip_static模塊_nginx
- 2022-04-18 python?commands模塊的適用方式_python
- 2022-03-09 c++中STL庫隊列詳細介紹_C 語言
- 2022-10-14 Redis緩存擊穿解決方案之互斥鎖
- 2022-05-06 golang excel數據寫入到sqlite3中
- 最近更新
-
- 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同步修改后的遠程分支