• 友链

  • 首页

  • 文章归档
h u a n b l o g
h u a n b l o g

欢

HI,Friend

04月
13
C#

枚举器

发表于 2022-04-13 • 字数统计 1967 • 被 1,760 人看爆

概念

用于依次返回请求的数组中的元素
通过调用对象的GetEnumerator方法,来获取一个对象枚举器

IEnumerator接口

支持对非泛型集合的简单迭代
包含3个函数成员:Current、MoveNext以及Reset
用法如:foreach
需引入using System.Collections; 
  • Current是返回序列中当前位置项的属性。
    • 它是只读属性。
    • 它返回object类型的引用,所以可以返回任何类型。
  • MoveNext是把枚举器位置前进到集合中下一项的方法。它也返回布尔值,指示新的位置是有效位置还是已经超过了序列的尾部。
    • 如果新的位置是有效的,方法返回true。
    • 如果新的位置是无效的(比如当前位置到达了尾部),方法返回false。
    • 枚举器的原始位置在序列中的第一项之前,因此MoveNext必须在第一次使用Current之前调用。
  • Reset是把位置重置为原始状态的方法。

所有非泛型枚举器的基接口

实例

模拟foreach循环

class Program
{
    static void Main(string[] args)
    {
        int[] MyArray = { 10, 11, 12, 13 };
        IEnumerator ie = MyArray.GetEnumerator();   //获取枚举器 
        while(ie.MoveNext())        //移到下一项
        {
            int i = (int)ie.Current;        //获取当前项
            Console.WriteLine(i);
        }

        Console.ReadKey();
    }
}

结果

10
11
12
13

IEnumerbale接口

公开枚举数,该枚举数支持在非泛型集合上进行简单迭代
可枚举类是指实现了IEnumerable接口的类
只有一个成员-GetEnumerator方法,返回对象的枚举类

语法

using System.Collections;
class MyClass:IEnumerable
{
    public IEnumerator GetEnumerator{
        ....
    }
}

实例

使用IEnumerable和IEnumerator

class ColorEnumerator : IEnumerator
{
    string[] _colors;
    int _position = -1;
    public ColorEnumerator(string[] theColors)
    {
        _colors = new string[theColors.Length];
        for(int i = 0; i < theColors.Length; i++)
        {
            _colors[i] = theColors[i];
        }
    }

    public object Current
    {
        get
        {
            if (_position == -1)
                throw new InvalidOperationException();
            if (_position >= _colors.Length)
                throw new InvalidOperationException();
            return _colors[_position];
        }
    }

    public bool MoveNext()
    {
        if (_position < _colors.Length - 1)
        {
            _position++;
            return true;
        }
        else
            return false;

    }

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

class Spectrum : IEnumerable
{
    string[] Colors = { "violet", "blue", "cyan", "green", "yellow", "orange", "red" };
    public IEnumerator GetEnumerator()
    {
        return new ColorEnumerator(Colors);
    }
}

class Program
{
    
    static void Main(string[] args)
    {
        Spectrum spectrum = new Spectrum();
        foreach(string color in spectrum)
        {
            Console.WriteLine(color);
        }

        Console.ReadKey();
    }
}

结果

violet
blue
cyan
green
yellow
orange
red

枚举泛型接口

IEnumerable<T>和IEnumerator<T>

泛型和非泛型区别

非泛型接口形式

  • IEnumerable接口的GetEnumerator方法返回实现IEnumerator枚举器类的实例;
  • 实现IEnumerator的类实现了Current属性,它返回object的引用,然后我们必须把它转化为实际类型的对象。

泛型接口形式

  • IEnumerable接口的GetEnumerator方法返回实现IEnumator的枚举器类的实例;
  • 实现IEnumerator的类实现了Current属性,它返回实际类型的对象,而不是object基类的引用。

泛型接口的枚举器是类型安全的,它返回实际类型的引用。

分享到:
迭代器
泛型
  • 文章目录
  • 站点概览
欢

网红 欢

你能抓到我么?

Email RSS
看爆 Top5
  • mac系统版本与Xcode版本有冲突 4,091次看爆
  • JAVA_HOME环境配置问题 3,741次看爆
  • AssetBundle使用 3,510次看爆
  • VSCode配置C++开发环境 3,264次看爆
  • Lua反射 3,141次看爆

Copyright © 2025 欢 粤ICP备2020105803号-1

由 Halo 强力驱动 · Theme by Sagiri · 站点地图