zoukankan      html  css  js  c++  java
  • 浅谈迭代器的使用

    一、关于迭代器

    迭代器是一种方法、get 访问器或运算符,它通过使用 yield 关键字对数组或集合类执行自定义迭代。yield 返回语句会导致源序列中的元素在访问源序列中的下一个元素之前立即返回给调用方。尽管您以方法的形式编写迭代器,但编译器会将其转换为一个实际上是状态机的嵌套类。只要客户端代码中的 foreach 循环继续进行,此类就会跟踪迭代器的位置。

    二、迭代器概述

    1、迭代器是可以返回相同类型的值的有序序列的一段代码。

    2、迭代器可用作方法、运算符或 get 访问器的代码体。

    3、迭代器代码使用 yield return 语句依次返回每个元素。yield break 将终止迭代。

    4、可以在类中实现多个迭代器。每个迭代器都必须像任何类成员一样有唯一的名称,并且可以在 foreach 语句中被客户端代码调用,如下所示:foreach(int x in SampleClass.Iterator2){}。

    5、迭代器的返回类型必须为 IEnumerable、IEnumerator、IEnumerable<(Of <(T>)>) 或 IEnumerator<(Of <(T>)>)。

    6、迭代器是 LINQ 查询中延迟执行行为的基础。

    三、迭代器的使用

    创建迭代器最常用的方法是对 IEnumerable 接口实现 GetEnumerator 方法

    下面先创建一个包含Name 和 Rate 属性的Contractor类。然后在创建一个Contractors,该类是Contractor对象的集合。最后通过可枚举模式以使用foreach循环对集合进行枚举。

     1 public class Contractor
     2     {
     3         private string name;
     4         private string rate;
     5 
     6         public Contractor(string Name,string Rate)
     7         {
     8             this.name = Name;
     9             this.rate = Rate;
    10         }
    11         /// <summary>
    12         /// 重写ToString
    13         /// </summary>
    14         /// <returns></returns>
    15         public override string ToString()
    16         {
    17             return string.Format("{0}[${1:.00}]",this.name,this.rate);
    18         }
    19 
    20     }
    View Code

     下面是Contractors类继承IEnumerable接口,实现了GetEnumerator方法

     1 public class Contractors:IEnumerable
     2     {
     3         private ArrayList item = new ArrayList();
     4         public IEnumerator GetEnumerator()
     5         {
     6             for (int index = 0; index < this.Count; index++)
     7             {
     8                 yield return this[index];
     9             }
    10         }
    11 
    12         /// <summary>
    13         /// 索引器
    14         /// </summary>
    15         /// <param name="index">索引</param>
    16         /// <returns></returns>
    17         public Contractor this[int index]
    18         {
    19             get
    20             {
    21                 return (Contractor)item[index];
    22             }
    23         }
    24 
    25         /// <summary>
    26         /// 集合中项的个数
    27         /// </summary>
    28         public int Count
    29         {
    30             get
    31             {
    32                 return this.item.Count;
    33             }
    34         }
    35 
    36         /// <summary>
    37         /// 添加项
    38         /// </summary>
    39         /// <param name="Name"></param>
    40         /// <param name="Rate"></param>
    41         /// <returns></returns>
    42         public int Add(string Name,string Rate)
    43         {
    44             return item.Add(new Contractor(Name,Rate));
    45         }
    46 
    47     }
    View Code

    下面是在main中调用方法:

     1 class Program 
     2     {
     3         static void Main(string[] args)
     4         {
     5             Contractors item = new Contractors();
     6             Contractors item2 = item;
     7             item.Add("张三", "120.02445");
     8             item.Add("李四","153");
     9             item.Add("王五","190");
    10             item.Add("赵六","120");
    11             foreach (Contractor c in item)
    12             {
    13                 Console.WriteLine(c.ToString());
    14             }
    15 
    16             Console.WriteLine("
    "+item[2]);
    17             Console.WriteLine( "1 and 2 :"+Comparer.Default.Compare("1","2"));
    18             Console.WriteLine("item2 and item hashtable value :"+item.GetHashCode()+"	"+item2.GetHashCode());
    19 
    20 
    21     
    22 
    23             
    24         }
    25     }
    View Code

     运行结果:

    个人小站:http://www.itguoguo.com

  • 相关阅读:
    UI系统的分类
    DSL的概念
    什么是“图灵完备”?
    UI系统的作用
    redis——持久化方式RDB与AOF分析
    Redis能做什么?不能做什么?
    PHP Ajax 跨域问题最佳解决方案
    charles和Fiddler感觉哪个更好用
    Fiddler工具使用介绍一
    Xshell出现要继续使用此程序必须应用到最新的更新或使用新版本
  • 原文地址:https://www.cnblogs.com/wangpf/p/3760810.html
Copyright © 2011-2022 走看看