首先是IEnumerable与IEnumerator的定义:

1.IEnumerable接口允许使用foreach循环,包含GetEnumerator()方法,可以迭代集合中的项。

2.IEnumerator接口是一个真正的集合访问器,它包含MoveNext()方法和Current属性,在foreach循环中,如果MoveNext()返回True,则就是用IEnumerator接口的Current属性来获取对象的一个引用,用于foreach循环。

3.如果要迭代一个类,可以使用GetEnumerator(),其返回类型是IEnumerator.

 如果要迭代一个类成员,则用IEnumerable.

下面的例子是迭代Person类中的类成员Ages,使用了IEnumerable。第二个例子则是迭代一个类,所以使用了IEnumerator作为返回值。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Collections;
  7.  
  8. namespace _10_5_5
  9. {
  10. public class person
  11. {
  12. private string name;
  13. private int age;
  14. public string Name
  15. {
  16. get
  17. {
  18. return name;
  19. }
  20. set
  21. {
  22. name = value;
  23. }
  24. }
  25. public int Age
  26. {
  27. get
  28. {
  29. return age;
  30. }
  31. set
  32. {
  33. age = value;
  34. }
  35. }
  36. public person(string PName, int PAge)
  37. {
  38. Name = PName;
  39. Age = PAge;
  40. }
  41. public static bool operator >(person a, person b)
  42. {
  43. if (a.Age > b.Age)
  44. return true;
  45. else
  46. return false;
  47. }
  48. public static bool operator <(person a, person b)
  49. {
  50. if (a.Age > b.Age)
  51. return false;
  52. else
  53. return true;
  54. }
  55. public static bool operator >=(person a, person b)
  56. {
  57. if (a.Age >= b.Age)
  58. {
  59. return true;
  60. }
  61. else
  62. return false;
  63. }
  64. public static bool operator <=(person a, person b)
  65. {
  66. if (a.Age <= b.Age)
  67. return true;
  68. else
  69. return false;
  70. }
  71. }
  72. public class People : DictionaryBase
  73. {
  74. public IEnumerable Ages//注意是IEnumerable
  75. {
  76. get
  77. {
  78. foreach (object person in Dictionary.Values)
  79. {
  80. yield return (person as person).Age;
  81. }
  82. }
  83. }
  84. public person[] GetOldest()
  85. {
  86. People oldPeople = new People();
  87. person oldPerson = null;
  88. person currentPerson;
  89. foreach (DictionaryEntry myPeople in Dictionary)
  90. {
  91. currentPerson = myPeople.Value as person;
  92. if (oldPerson == null)
  93. {
  94. oldPerson = currentPerson;
  95. oldPeople.Add(oldPerson);
  96. }
  97. else
  98. {
  99. if (currentPerson > oldPerson)
  100. {
  101. oldPeople.Clear();
  102. oldPeople.Add(currentPerson);
  103. oldPerson = currentPerson;
  104. }
  105. else
  106. {
  107. if (currentPerson >= oldPerson)
  108. {
  109. oldPeople.Add(currentPerson);
  110. }
  111. }
  112. }
  113. }
  114. person[] oldestPeopleArray = new person[oldPeople.Count];
  115. int copyIndex = ;
  116. foreach (DictionaryEntry p in oldPeople)
  117. {
  118. oldestPeopleArray[copyIndex] = p.Value as person;
  119. copyIndex++;
  120. }
  121. return oldestPeopleArray;
  122. }
  123. public void Add(person p)
  124. {
  125. Dictionary.Add(p.Name, p);
  126. }
  127. public person this[string SName]
  128. {
  129. get
  130. {
  131. return (person)Dictionary[SName];
  132. }
  133. set
  134. {
  135. Dictionary[SName] = value;
  136. }
  137. }
  138.  
  139. }
  140. class Program
  141. {
  142. static void Main(string[] args)
  143. {
  144. person a = new person("Jack", );
  145. person b = new person("Json", );
  146. People s = new People();
  147. s.Add(a);
  148. s.Add(b);
  149. foreach(int age in s.Ages)
  150. {
  151. Console.WriteLine("{0}\t", age);
  152. }
  153. Console.ReadKey();
  154. }
  155. }
  156. }

下面是自定义的一个迭代器的例子:

Primer.CS

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Ch11Ex03_Exam
  9. {
  10. public class Primes
  11. {
  12. private long min;
  13. private long max;
  14. public Primes():this(,)
  15. {
  16.  
  17. }
  18. public Primes(long minNum,long maxNum)
  19. {
  20. if(minNum<)
  21. {
  22. min=;
  23. }else{
  24. min = minNum;
  25. }
  26. max = maxNum;
  27. }
  28. public IEnumerator GetEnumerator()//返回的是IEnumerator
  29. {
  30. for(long i=min;i<max;i++)
  31. {
  32. int flag = ;
  33. for(long j=;j<Math.Sqrt(min);j++)
  34. {
  35. if(i%j==)
  36. {
  37. flag = ;
  38. break;
  39. }
  40. }
  41. if(flag==)
  42. {
  43. yield return i;
  44. }
  45. }
  46. }
  47. }
  48. }

Program.CS:

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Ch11Ex03_Exam
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. Primes s = new Primes(, );
  15. foreach(long i in s)
  16. {
  17. Console.WriteLine("{0}\t", i);
  18. }
  19. Console.ReadKey();
  20. }
  21. }
  22. }

关于迭代器中IEnumerable与IEnumerator的区别的更多相关文章

  1. 在C#中IEnumerable与IEnumerator

    对于很多刚开始学习C#同学来说经常会遇到IEnumerable这个关键字,enumerate在字典里的解释是列举,枚举,因此可想而知这个关键字肯定是和列举数据有关的操作. public interfa ...

  2. C#中IEnumerable和IEnumerator区别

    IEnumerator:是一个真正的集合访问器,提供在普通集合中遍历的接口,有Current,MoveNext(),Reset(),其中Current返回的是object类型.IEnumerable: ...

  3. C#--IEnumerable 与 IEnumerator 的区别

    一. IEnumerator 解释:它是一个的集合访问器,使用foreach语句遍历集合或数组时,就是调用 Current.MoveNext()的结果. // 定义如下public interface ...

  4. C# ~ 从 IEnumerable / IEnumerator 到 IEnumerable<T> / IEnumerator<T> 到 yield

    IEnumerable / IEnumerator 首先,IEnumerable / IEnumerator 接口定义如下: public interface IEnumerable /// 可枚举接 ...

  5. C#中的 IList, ICollection ,IEnumerable 和 IEnumerator

    IList, ICollection ,IEnumerable 很显然,这些都是集合接口的定义,先看看定义: // 摘要: // 表示可按照索引单独访问的对象的非泛型集合. [ComVisible(t ...

  6. C#编程之IList<T>、List<T>、ArrayList、IList, ICollection、IEnumerable、IEnumerator、IQueryable 和 IEnumerable的区别

    额...今天看了半天Ilist<T>和List<T>的区别,然后惊奇的发现使用IList<T>还是List<T>对我的项目来说没有区别...  在C#中 ...

  7. 细说 C# 中的 IEnumerable和IEnumerator接口

    我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq to Object中要返回IEnumerable? 接下来,先开始我们的正 ...

  8. 转载IEnumerable与IEnumerator区别

    public interface IEnumerable {     IEnumerator GetEnumerator(); }   public interface IEnumerator {   ...

  9. C#中IEnumerable、ICollection、IList、IQueryable 、IQueryable 、List之间的区别

    一:一个简单的例子 int[] myArray = { 1, 32, 43, 343 }; IEnumerator myie = myArray.GetEnumerator(); myie.Reset ...

随机推荐

  1. web项目总结

    web项目 Webroot下面的index.jsp页面的内容: <%@ page language="java" pageEncoding="UTF-8" ...

  2. Maven3路程(三)用Maven创建第一个web项目(2)servlet演示

    上一章用Maven新建了web项目成功后,本文演示在此基础上应用servlet. 1.首先修改pom.xml文件,添加servlet依赖 <project xmlns="http:// ...

  3. Android中AutoCompleteTextView的使用

    1.http://blog.csdn.net/FX_SKY/article/details/9326129 此博客讲解了auto组件如何使用baseAdapter来扩展功能,推荐参照此博客写demo ...

  4. BugTracker.NET的配置

    需求管理+任务管理+bug管理+看板管理 要求一定要简单,切忌不要太复杂 之前用的禅道的,功能虽然很强大,但是忒复杂了,用不下去. 几点需要注意的地方: 1.web.config里邮件的地方有好几个地 ...

  5. c# 垮线程调用控件

    http://www.cnblogs.com/TankXiao/p/3348292.html

  6. AX 与Citrix打印机问题

    国外文章,抄个链接,备查 http://blogs.msdn.com/b/axsupport/archive/2010/07/06/ax-2009-citrix-amp-terminal-server ...

  7. iOS8中使用CoreLocation定位[转]

    本文转自:http://blog.devzeng.com/blog/ios8-corelocation-framework.html iOS8以前使用CoreLocation定位 1.首先定义一个全局 ...

  8. java之对象转型2

    public class TestCasting2{ public static void main(String args[]){ TestCasting2 test2= new TestCasti ...

  9. (OSP)外包工单关工单失败

    会计同事反映,在关几个外包(OSP)工单时,系统报错.错误讯息如下.检查错误讯息,发现Number of jobs failed in Delivered Quantity : 2.检查工单数据,均无 ...

  10. html中css三种常见的样式选择器 zz

    1:标签选择器 标签选择器,是所有带有某种标签的都生效.这里以p为例,也就是所有的带有p标记的都会这样的样式 <html><head><styletype="t ...