• 【c#】IEnumerable、IEnumerator、yield、Unity协程相关


    IEnumerable

    public interface IEnumerable
        {
            IEnumerator GetEnumerator();
        }
    
    • 1
    • 2
    • 3
    • 4

    IEnumerable非常简单,就一个GetEnumerator()方法,但是这个方法的返回值却是一个IEnumerator对象,从注释我们可以得知IEnumerable代表继承此接口的类可以获取一个IEnumerator实现枚举这个类中包含的集合中的元素的功能(比如List/ArrayList/Dictionary等继承了IEnumeratble接口的类)

    IEnumerator

     public interface IEnumerator
        {
            object Current { get; }
            bool MoveNext();
            void Reset();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    IEnumerator有一个object类型的属性,还有一个返回值为bool值的MoveNext方法和一个无返回值的Reset方法

    IEnumerator 与 IEnumerable使用的案例

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace dotnetLearn.集合学习
    {
    
        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()
            {
                Console.WriteLine("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
                {
                    Console.WriteLine("foreach调用  Current");
                    return myList[index];
                }
            }
    
            public bool MoveNext()
            {
                Console.WriteLine("foreach调用  MoveNext");
                if (index < myList._data.Length - 1)
                {
                    index++;
                    return true;
                }
                index = -1;
                return false;
            }
    
            public void Reset()
            {
    
            }
        }
    
        public class IEnumerator01
        {
            public void test()
            {
                MyList my = new MyList();
    
                foreach (var item in my)
                {
                    Console.WriteLine(item);
                }
                Console.ReadKey();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84

    通过输出结果,我们可以发现,foreach在运行时会先调用MyList的GetIEnumerator函数获取一个MIEnumtor,之后通过循环调用MIEnumtor的MoveNext函数,index后移,更新Current属性,然后返回Current属性,直到MoveNext返回false。

  • 相关阅读:
    Elasticsearch部署中的两大常见问题及其解决方案
    深入理解final关键字
    RPC模型(未完成)
    Cookie和Session
    【音视频】FLV封装格式
    深度学习阿丘科技AIDI标注工具使用(2.3版本)
    Gitlab 数据备份和还原(填坑版)
    CSS calc() 使用指南
    MyBioSource p53 肿瘤抑制蛋白 (TP53),多克隆抗体
    加载器、反射、xml、DTD、schema、注解、单元测试
  • 原文地址:https://blog.csdn.net/qq_22849251/article/details/126745313