zoukankan      html  css  js  c++  java
  • 迭代器

      迭代器是 C# 2.0 中的新功能。迭代器是方法、get 访问器或运算符,它使您能够在结构中支持 foreach 迭代,而不必实现整个 IEnumerable 接口。您只需提供一个迭代器,即可遍历类中的数据结构。当编译器检测到迭代器时,它将自动生成 IEnumerable 或 IEnumerable<T> 接口的 CurrentMoveNext 和 Dispose 方法。

    1.迭代器概述

      迭代器是可以返回相同类型的值的有序序列的一段代码。
      迭代器可用作方法、运算符或 get 访问器的代码体。
      迭代器代码使用 yield return 语句依次返回每个元素。yield break 将终止迭代。有关更多信息,请参见 yield。
      可以在类中实现多个迭代器。每个迭代器都必须像任何类成员一样有唯一的名称,并且可以在 foreach 语句中被客户端代码调用,如下所示:foreach(int x in SampleClass.Iterator2){}
      迭代器的返回类型必须为 IEnumerable、IEnumerator、IEnumerable<T> 或 IEnumerator<T>。
      yield 关键字用于指定返回的值。到达 yield return 语句时,会保存当前位置。下次调用迭代器时将从此位置重新开始执行。
      迭代器对集合类特别有用,它提供一种简单的方法来迭代不常用的数据结构(如二进制树)。

    实例1

        class Program
        {
            static void Main(string[] args)
            {
                // Create an instance of the collection class
                DaysOfTheWeek week = new DaysOfTheWeek();
    
                // Iterate with foreach
                foreach (string day in week)
                {
                    System.Console.Write(day + " ");
                }
                Console.ReadLine();
            }
        }
    
        public class DaysOfTheWeek : System.Collections.IEnumerable
        {
            string[] m_Days = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" };
    
            public System.Collections.IEnumerator GetEnumerator()
            {
                for (int i = 0; i < m_Days.Length; i++)
                {
                    yield return m_Days[i];
                }
            }
        }

     输出:Sun Mon Tue Wed Thr Fri Sat

    实例2

        class Program
        {
            static void Main(string[] args)
            {
                // Create an instance of the collection class
                DaysOfTheWeek week = new DaysOfTheWeek();
    
                // Iterate with foreach
                foreach (string day in week)
                {
                    System.Console.Write(day + ", ");
                }
                Console.ReadLine();
            }
        }
    
        public class DaysOfTheWeek : System.Collections.IEnumerable
        {
            //在 foreach 循环的每次后续迭代(或对 IEnumerator.MoveNext 的直接调用)中,下一个迭代器代码体将从前一个 yield 语句之后开始,并继续下一个语句直至到达迭代器体的结尾或遇到 yield break 语句。
            public System.Collections.IEnumerator GetEnumerator()
            {
                yield return "1";
                yield return "2";
                yield return "3";
                yield return "4";
                yield return "5";
                yield return "6";
                yield return "7";
            }
        }

    输出:1, 2, 3, 4, 5, 6, 7,

    实例3 为整数列表创建迭代器块

        class Program
        {
            static void Main(string[] args)
            {
                SampleCollection col = new SampleCollection();
    
                // Display the collection items:
                System.Console.WriteLine("Values in the collection are:");
                foreach (int i in col.BuildCollection())
                {
                    System.Console.Write(i + " ");
                }
                Console.ReadLine();
            }
    
            // Declare the collection:
            public class SampleCollection
            {
                public int[] items;
    
                public SampleCollection()
                {
                    items = new int[5] { 5, 4, 7, 9, 3 };
                }
    
                public System.Collections.IEnumerable BuildCollection()
                {
                    for (int i = 0; i < items.Length; i++)
                    {
                        yield return items[i];
                    }
                }
            }
        }
  • 相关阅读:
    如何提高软件可维护性
    UML系列 (二)四种关系
    软件工程
    软件工程需求规格说明书
    机房收费系统可行性分析报告
    ThreadStaticAttribute 的使用
    WPF:Border 控件
    几篇介绍在页面中引用脚本文件的技术文档
    收集两篇介绍 Fildder 的文章
    收集三篇关于数据库主键设计的文章
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/7993971.html
Copyright © 2011-2022 走看看