C#学习相关系列之自定义遍历器

在C#中,自定义遍历器需要实现IEnumerable接口和IEnumerator接口。其中,IEnumerable接口包含一个GetEnumerator方法,该方法返回一个IEnumerator接口的实例,而IEnumerator接口包含Current、MoveNext和Reset方法。

IEnumerable:IEnumerable是一个接口,它代表一个可以迭代的集合。这意味着可以使用IEnumerator:foreach循环遍历IEnumerable对象中的元素。

在C#中,IEnumerator是.NET框架中的一个接口,它用于在集合中遍历元素。这个接口通常与使用foreach循环来遍历集合的代码一起使用。

IEnumerator接口定义了两个主要的方法:MoveNext()Reset()

  • MoveNext()方法用于将迭代器移动到集合中的下一个元素。如果集合中没有更多的元素,则该方法将返回false
  • Reset()方法用于重置迭代器,使其重新回到集合的起始位置。

此外,IEnumerator接口还定义了一个Current属性,它返回当前迭代器位置的元素。

一、yield关键字生成迭代器

cs 复制代码
using System;  
using System.Collections.Generic;  
  
class Program  
{  
    static void Main()  
    {  
        foreach (int number in GetNumbers())  
        {  
            Console.WriteLine(number);  
        }  
    }  
  
    static IEnumerable<int> GetNumbers()  
    {  
        for (int i = 0; i < 10; i++)  
        {  
            yield return i;  
        }  
    }  
}

只有IEnumerable才能通过foreach被遍历。

二、关于**IEnumerator的用法示例**

cs 复制代码
List<string> fruits = new List<string> { "Apple", "Banana", "Cherry" };  
  
IEnumerator enumerator = fruits.GetEnumerator();  
  
while (enumerator.MoveNext())  
{  
    string fruit = (string)enumerator.Current;  
    Console.WriteLine(fruit);  
}

通过这个例子可以看出来 IEnumerable是一个可以迭代的集合,而**IEnumerator是定义foreach迭代器。**

三、自定义一可以遍历的类

cs 复制代码
 public class Program
    {
        static void Main(string[] args)
        {
            student st = new student();
            st.add(2);
            st.add(3);
            st.add(100);
            foreach (var item in st)
            {
                Console.WriteLine(item);
            }
            Console.Read();
        }
    }
    public class student : IEnumerable, IEnumerator

    {
        private List<int> _data;
        private int position = -1;
        private List<int> data = new List<int>();
        public student()
        { 
        }
        public student(List<int> data)
        {
            this._data = data;
        }

        public object Current => _data[position];

        public void add(int num)
        {
            data.Add(num);
        }
        public IEnumerator GetEnumerator()
        {
            return new student(data);
        }

        public bool MoveNext()
        {
            position++;
            return position < _data.Count;
        }

        public void Reset()
        {
            position = -1;
        }
    }

代码2

cs 复制代码
using System;
using System.Collections;

public class MyList : IEnumerable
{
    private int[] data = { 1, 2, 3, 4, 5 };

    public IEnumerator GetEnumerator()
    {
        return new MyEnumerator(data);
    }
}

public class MyEnumerator : IEnumerator
{
    private int[] data;
    private int position = -1;

    public MyEnumerator(int[] data)
    {
        this.data = data;
    }

    public object Current
    {
        get
        {
            return data[position];
        }
    }

    public bool MoveNext()
    {
        position++;
        return (position < data.Length);
    }

    public void Reset()
    {
        position = -1;
    }
}

// 使用自定义遍历器
class Program
{
    static void Main(string[] args)
    {
        MyList list = new MyList();
        foreach (int i in list)
        {
            Console.WriteLine(i);
        }
    }
}
相关推荐
Chester_19992 小时前
CSP202203C.计算资源调度器
开发语言·数据结构·c++·蓝桥杯
EXI-小洲3 小时前
Java 操作 Word:字符串替换、图片插入、动态生成表格与API接口下载
java·开发语言·spring boot·word
会周易的程序员4 小时前
给 PLC 写一个字节码虚拟机:STVM 虚拟机架构设计
开发语言·c++·虚拟机·软plc·iec61131·stvm
机器视觉知识推荐、就业指导4 小时前
为什么老说“纯 Qt 没啥就业市场”?
开发语言·qt
telepan4 小时前
Qt 开发避坑与性能指南:如何优雅、安全地定义全局常量字符串?
开发语言·qt
m0_695251494 小时前
Qt MaintenanceTool 使用国内镜像加速下载(超详细教程)
开发语言·qt
是隼人4 小时前
buuctf-pwn xdctf2015_pwn200题解(学习过程持续更新)
c语言·学习·安全·pwn入门·ctf入门
大衛說5 小时前
《Python 从入门到精通》系列总览与学习路线
开发语言·python·学习
吃好睡好便好6 小时前
查找函数的使用
学习·算法·matlab·生活·查找函数
学运维的Kysan6 小时前
暑假运维学习打卡第二十九天8.29
学习