zoukankan      html  css  js  c++  java
  • C#学习笔记之——枚举器和迭代器

    目录

    枚举器和迭代器

    枚举器和可枚举类型

    IEnumerator接口

    IEnumerable接口

    泛型枚举接口

    迭代器

    迭代器块

    使用迭代器来创建枚举器

    使用迭代器来创建可枚举类型

    常见迭代器模式

    产生多个可枚举类型

    将迭代器作为属性

    迭代器的实质


    枚举器和迭代器

    枚举器和可枚举类型

                int[] arr = { 1, 2, 3, 4 };
    
                foreach (int i in arr)
                {
                    Console.WriteLine(arr[i-1]);
                }

    利用foreach遍历数组,循环打印这些值。数组可以这样做的原因是数组可以按需提供一个枚举器(enumerator)的对象。枚举器可以依次返回请求的数组中的元素。

    对于有枚举器的类型而言,需要一个方法获取它,获取的方法是调用对象的GetEnumerator方法。实现GetEnumerator方法的类型叫可枚举类型(enumerable type或enumerable)。数组是可枚举类型。

    foreach结构设计用来和可枚举类型一起使用。

    IEnumerator接口

    Current返回序列中当前位置项的属性。

    • 它是只读的
    • 它返回object类型的引用,所以可以返回任何类型。

    MoveNext是把枚举器位置前进到集合中下一项的方法。它也返回布尔值,指示新的位置是有效位置还是已经超过序列的尾部。

    • 如果新的位置有效,返回true,反之,则返回false。
    • 枚举器的原始位置在序列中的第一项之前,阴恻MoveNext必须在第一次使用Current之前调用。

    Reset是把位置重置为原始状态的方法。

                int[] arr = { 1, 2, 3, 4 };
    
                IEnumerator enu = arr.GetEnumerator();
    
                foreach (int i in arr)
                {
                    Console.WriteLine(arr[i-1]);
                }
    
                Console.WriteLine();
    
                while (enu.MoveNext())
                {
                    int i = (int)enu.Current;
                    Console.WriteLine("{0}", i);
                }

    IEnumerable接口

    可枚举类是指实现了IEnumerable接口的类。IEnumerable接口只有一个成员——GetEnumerator方法,它返回对象的枚举器。

    使用IEnumerable和IEnumerator的实例

    using System;
    using System.Collections;
    
    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                MyColors myColors = new MyColors();
                foreach (string color in myColors)
                {
                    Console.WriteLine(color);
                }
    
            }
        }
    
        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];
                }
            }
    
            //实现Current
            public object Current
            {
                get
                {
                    if( position == -1)
                    {
                        throw new InvalidOperationException();
                    }
                    if (position >= colors.Length)
                    {
                        throw new InvalidOperationException();
                    }
                    return colors[position];
                }
            }
    
            //实现MoveNext
            public bool MoveNext()
            {
                if (position < colors.Length - 1)
                {
                    position++;
                    return true;
                }
                else
                    return false;
            }
    
            //实现Reset
            public void Reset()
            {
                position = -1;
            }
        }
    
        class MyColors:IEnumerable
        {
            readonly string[] Colors = { "violet", "blue", "cyan", "green", "yellow", "orange", "red" };//红橙黄绿蓝靛紫
    
            public MyColors()
            {
            }
    
            public IEnumerator GetEnumerator()
            {
                return new ColorEnumerator(Colors);//枚举器类的实例
            }
        }
       
    }
    

    泛型枚举接口

    IEnumerable<T>和IEnumerator<T>。

    迭代器

    C#提供的更简单的创建枚举器和可枚举类型的方式。实质上,编译器将帮我们创建。这种结构叫做迭代器(iterator)

    迭代器块

    使用迭代器来创建枚举器

    使用迭代器来创建可枚举类型

    常见迭代器模式

    产生多个可枚举类型

    将迭代器作为属性

    迭代器的实质

  • 相关阅读:
    LeetCode——加油站
    LeetCode——分发糖果
    LeetCode——单词拆分 ii
    LeetCode—— 单词拆分
    LeetCode——重排链表
    IAR ARM、IAR STM8、IAR MSP430共用一个IDE
    OSAL多任务资源分配机制
    Win7系统Matlab2013a安装.m文件不自动关联到MATLAB.exe解决方法
    Java SE/ME/EE的概念介绍
    STL,ATL,WTL之间的联系和区别
  • 原文地址:https://www.cnblogs.com/AlinaL/p/12852141.html
Copyright © 2011-2022 走看看