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);
        }
    }
}
相关推荐
BUG研究员_2 小时前
Runnable与LCEL
开发语言·人工智能·python
云深处@3 小时前
【数据库】MySQL 入门
数据库·学习·mysql
HyperAI超神经3 小时前
【vLLM 学习】Disaggregated Prefill
人工智能·深度学习·学习·vllm
牛艺翔3 小时前
C++基础
开发语言·c++
键盘会跳舞3 小时前
C++ :容器适配器stack源码级拆解
开发语言·c++··stack·先进后出
天吾cc3 小时前
RTT-IO设备模型
单片机·嵌入式硬件·学习
美味蛋炒饭.3 小时前
Git 版本控制(下)
开发语言·git·学习·总结·后端开发
yanaiding4 小时前
C# WPF 独立开发看图工具 PixSight 经验介绍(二)—— 水印模块开发实录
图像处理·c#·wpf·个人开发
呼噜想睡觉4 小时前
Bootstrap 组件之响应式导航条实现
学习
我星期八休息4 小时前
网络编程—网络层
开发语言·前端·网络·人工智能·智能路由器