关键词搜索

源码搜索 ×
×

C#基础学习--枚举器和迭代器

发布2023-04-19浏览758次

详情内容

目录

枚举器和可枚举类型

 IEnumerator 接口

IEnumerable 接口

实现 IEnumerable 和 IEnumerator的示例

 泛型枚举接口

迭代器

迭代器块

使用迭代器来创建枚举器

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

常见迭代器模式

产生多个可枚举类型

 将迭代器作为属性

迭代器实质


枚举器和可枚举类型

数组按需提供一个叫做枚举器的对象。枚举器可以依次返回请求数组中的元素。

调用对象的GetEnumerator 方法可以获取一个对象枚举器,可以知道枚举器的类型。实现GetEnumerator方法的类型叫做 可枚举类型。数组是可枚举数组

foreach 结构设计用来和可枚举类型一起使用。只要它遍历的对象是可枚举类型,就可以执行如下操作:

 IEnumerator 接口

实现了 IEnumerator 接口的枚举器包含3个函数成员:Current ,MoveNext, Reset

枚举器与序列中的当前项保持联系的方式完全取决于实现,可以通过对象引用,索引值或其他方式来实现

IEnumerable 接口

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

实现 IEnumerable 和 IEnumerator的示例

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Csharpzuoye
  6. {
  7. class ColorEnumerator : IEnumerator
  8. {
  9. string[] _colors;
  10. int _position = -1;
  11. public ColorEnumerator(string[] theColors) //构造函数
  12. {
  13. _colors = new string[theColors.Length];
  14. for (int i = 0; i < theColors.Length; i++)
  15. _colors[i] = theColors[i];
  16. }
  17. public object Current //实现Current
  18. {
  19. get
  20. {
  21. if (_position == -1)
  22. throw new InvalidOperationException();
  23. if (_position >= _colors.Length)
  24. throw new InvalidOperationException();
  25. return _colors[_position];
  26. }
  27. }
  28. public bool MoveNext() //实现MoveNext
  29. {
  30. if(_position < _colors.Length - 1)
  31. {
  32. _position++;
  33. return true;
  34. }
  35. else
  36. {
  37. return false;
  38. }
  39. }
  40. public void Reset()
  41. {
  42. _position = -1;
  43. }
  44. }
  45. class Spectrum : IEnumerable
  46. {
  47. string[] Colors = { "violet", "blue", "cyan", "green", "yellow", "orange", "red" };
  48. public IEnumerator GetEnumerator()
  49. {
  50. return new ColorEnumerator(Colors);
  51. }
  52. }
  53. class Program
  54. {
  55. static void Main()
  56. {
  57. Spectrum spectrum = new Spectrum();
  58. foreach (string color in spectrum)
  59. Console.WriteLine(color);
  60. }
  61. }
  62. }

 泛型枚举接口

IEnumerable<T> 和 IEnumerator<T> 泛型

  泛型与非泛型的差别:

非泛型接口的实现不是类型安全的,它们返回object 类型的引用,然后必须转化为实际类型。

而泛型接口的枚举器是类型安全的,它返回实际类型的引用。如果创建自己的可枚举类,应该实现这些泛型接口

迭代器

我们可以把手动编码的可枚举类型和枚举器替换为有迭代器生成的可枚举类型和枚举器

迭代器块

迭代器块是有一个或多个 yield' 语句的代码块。下面三种任意一种都可以是迭代器块

迭代器块与其他代码块不同。其他块包含的语句被当作是命令式的,需要先执行代码块的第一个语句,然后执行后面的语句,最后离开块。

迭代器块不是需要在同一时间执行的一串命令式命令,而是描述了希望编译器为我们创建的枚举器类的行为。

编译器得到有关如何枚举项的描述后,使用它来构建包含所有需要的方法和属性实现的枚举器类。结果类被嵌套包含在迭代器声明的类中

使用迭代器来创建枚举器

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Csharpzuoye
  6. {
  7. class MyClass
  8. {
  9. public IEnumerator<string> GetEnumerator()
  10. {
  11. return BlackAndWhite(); //返回枚举器
  12. }
  13. //返回枚举器
  14. public IEnumerator<string> BlackAndWhite() //迭代器
  15. {
  16. yield return "black";
  17. yield return "gray";
  18. yield return "white";
  19. }
  20. }
  21. class Program
  22. {
  23. static void Main()
  24. {
  25. MyClass mc = new MyClass();
  26. foreach (string shade in mc) //使用MyClass的实例
  27. Console.WriteLine(shade);
  28. }
  29. }
  30. }

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

在本节中我们使用迭代器来创建可枚举类型,而不是枚举器

图中左边的迭代器代码演示了它的返回类型是IEnumerable<string> 

图中右边演示了他有一个嵌套类实现了IEnumerator<string> 和 IEnumerator<string>

常见迭代器模式

产生多个可枚举类型

 将迭代器作为属性

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Csharpzuoye
  6. {
  7. class Spectrum
  8. {
  9. bool _listFromUVtoIR;
  10. string[] colors = { "violet", "blue", "cyan", "green", "yellow", "orange", "red" };
  11. public Spectrum(bool listFromUVtoIR)
  12. {
  13. _listFromUVtoIR = listFromUVtoIR;
  14. }
  15. public IEnumerator<string> GetEnumerator()
  16. {
  17. return _listFromUVtoIR ? UVtoIR : IRtoUV;
  18. }
  19. public IEnumerator<string> UVtoIR
  20. {
  21. get
  22. {
  23. for (int i = 0; i < colors.Length; i++)
  24. yield return colors[i];
  25. }
  26. }
  27. public IEnumerator<string> IRtoUV
  28. {
  29. get
  30. {
  31. for (int i = colors.Length - 1; i >= 0; i--)
  32. yield return colors[i];
  33. }
  34. }
  35. }
  36. class Program
  37. {
  38. static void Main()
  39. {
  40. Spectrum startUV = new Spectrum(true);
  41. Spectrum startIR = new Spectrum(false);
  42. foreach (string color in startUV)
  43. Console.Write("{0} ", color);
  44. Console.WriteLine();
  45. foreach (string color in startIR)
  46. Console.Write("{0} ", color);
  47. Console.WriteLine();
  48. }
  49. }
  50. }

迭代器实质

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载