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

简介: 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关键字生成迭代器

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的用法示例

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迭代器。

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

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

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);
        }
    }
}
相关文章
|
1月前
|
Java 调度 C#
C#学习系列相关之多线程(一)----常用多线程方法总结
C#学习系列相关之多线程(一)----常用多线程方法总结
|
1月前
|
C#
C#学习相关系列之数据类型类的三大特性(二)
C#学习相关系列之数据类型类的三大特性(二)
|
1月前
|
C#
C#学习相关系列之yield和return的区别
C#学习相关系列之yield和return的区别
|
1月前
|
C#
C#学习相关系列之数组---常用方法使用(二)
C#学习相关系列之数组---常用方法使用(二)
|
1月前
|
存储 C#
C#学习系列相关之数组(一)---数组的定义与使用
C#学习系列相关之数组(一)---数组的定义与使用
|
1月前
|
C#
C#学习相关系列之常用符号介绍
C#学习相关系列之常用符号介绍
|
1月前
|
安全 编译器 C#
C#学习相关系列之多线程---lock线程锁的用法
C#学习相关系列之多线程---lock线程锁的用法
|
1月前
|
C#
C#学习相关系列之多线程---ConfigureAwait的用法
C#学习相关系列之多线程---ConfigureAwait的用法
|
1月前
|
C#
C#学习相关系列之多线程---TaskCompletionSource用法(八)
C#学习相关系列之多线程---TaskCompletionSource用法(八)
|
1月前
|
开发框架 .NET C#
C#学习相关系列之Linq用法---where和select用法(二)
C#学习相关系列之Linq用法---where和select用法(二)