C#枚举器/迭代器
一、枚举器
1、为什么foreach可以顺序遍历数组?
因为foreach可以识别可枚举类型,通过访问数组提供的枚举器对象来识别数组中元素的位置从而获取元素的值并打印出来。
2、什么是枚举器?可枚举类型?
枚举结构里元素都是默认排序的,可以依靠识别元素的位置来获取值。可以把枚举器看做是集合的一个方法对象,他可以
依次返回所有的元素。而诸如此类给元素排好序的集合类型都可以算作可枚举类型。
3、具体过程如何实现?

二、可枚举类
枚举器继承了IEnumerable接口,包含了三个函数成员:Current、MoveNext、Reset。

可枚举类是指实现类IEnumerable接口的类,而这个接口只有一个成员——GetEnumerator方法,它负责返回对象的枚举器。
using System;
using System.Collections; 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];
} public object Current // 实现Current
{
get
{
if (_position == -1 )
throw new InvalidOperationException();
if (_position >= _colors.Length )
throw new InvalidOperationException();
return _colors[_position];
}
} public bool MoveNext() // 实现MoveNext
{
if ( _position < _colors.Length - 1 )
{
_position ++;
return true;
}
else
return false;
} public void Reset() // 实现Reset
{
_position = -1;
}
} class Spectrum : IEnumerable
{
string[] Colors = { "violet", "blue", "cyan", "green","yellow", "orange", "red" }; public IEnumerator CetEnumerator()
{
return new ColorEnumerator( Colors );
}
} class Program
{
static void Main()
{
Spectrum spectrum = new Spectrum(); foreach( string color in spectrum )
Console.Writeline( color );
}
}
三、迭代器
一种便捷方式来创建枚举器跟迭代器,省略了手动编码实现可枚举类型跟枚举器。
常用的迭代器模式有两种:
1、不实现GetEnumerator方法,直接调用迭代器方法。
class Spectrum
{
string[] colors ={ "violet", "blue", "cyan”, “green", "yellow", "orange", "red" }; // 返回一个可枚举类型
public IEnumerable<string> UVtoIR()
{
for ( int i = 0; i < colors.Length; i++)
yield return colors[i]);
}
// 返回一个可枚举类型
public IEnumerable<string> IRtoU()
{
for ( int i = colors.Length -1; i >= 0; i--)
yleld return colors[i];
}
} class Program
{
static void Main()
{
Spectrum spectrum = new Spectrum(); foreach ( string color in spectrum.UVtoIR())
Console.Write("{0}",color );
Console.WriteLine(); foreach ( string color in spectrum.IRtoUV())
Console.Write("{O}",color );
Console.Writeline();
}
}
2、实现GetEnumerator方法,让类可枚举,调用迭代器的时候会自动生成IEnumerable的类实例
class Spectrum
{
bool _listFromUVtoIR;
string[] colors = { "violet", "blue", "cyan", "green","yellow", "orange", "red"); public Spectrum( bool listFromVtoIR )
{
_listFromlVtoIR = listFromUVtoIR;
} public IEnumerator<string> GetEnumerator()
{
return _listFromlVtoIR ? UVtoIR : IRtoUV;
} public IEnumerator<string> UVtoIR
{
get
{
for ( int i = 0; i < colors.Length; i++ )
yield return colors[i];
}
} public IEnumerator<string> IRtoUV
{
get
{
for ( int i = colors.Length - 1; i >= 0; i--)
yield return colors[i];
}
}
} class Program
{
static void Main()
{
Spectrum startUV = new Spectrum( true );
Spectrum startIR = new Spectrum( false ); foreach ( string color in startuV )
Console.Write("{0}", color );
Console.Writeline(); foreach ( string color in startIR )
Console.Write("{0}", color );
Console.WriteLine();
}
}
C#枚举器/迭代器的更多相关文章
- C#-14 枚举器和迭代器
一 枚举器和可枚举类型 当我们为数组使用foreach语句时,这个语句为我们依次取出了数组中的每一个元素. var arrInt = new int[] { 11, 12, 13, 14 }; for ...
- ruby迭代器枚举器
迭代器一个迭代器是一个方法,这个方法里面有yield语句,使用了yield的方法叫做迭代器,迭代器并非一定要迭代,与传递给这个方法的块进行数据传输 yield将数据传给代码快,代码块再把数据传输给yi ...
- ruby中迭代器枚举器的理解
参考<ruby编程语言>5.3迭代器和可枚举对象 迭代器一个迭代器是一个方法,这个方法里面有yield语句,这个方法里的yield语句,与传递给这个方法的块进行数据传输 yield将数据传 ...
- ruby迭代器iterator和枚举器Enumerator
编写自定义的迭代器 The defining feature of an iterator method is that it invokes a block of code associatedwi ...
- C#图解教程 第十八章 枚举器和迭代器
枚举器和迭代器 枚举器和可枚举类型 foreach语句 IEnumerator接口 使用IEnumerable和IEnumerator的示例 泛型枚举接口迭代器 迭代器块使用迭代器来创建枚举器使用迭代 ...
- C# 枚举器和迭代器
一.枚举器(enumerator)和可枚举类型(enumeration) 我们都知道foreach语句可以用来遍历数组中的元素,但你有没有想过为什么它可以被foreach处理呢? 这是因为数组可以按需 ...
- C#知识点-枚举器和迭代器
一.几个基本概念的理解 问题一:为什么数组可以使用foreach输出各元素 答:数组是可枚举类型,它实现了一个枚举器(enumerator)对象:枚举器知道各元素的次序并跟踪它们的位置,然后返回请求的 ...
- 设计模式 - 适配器模式(adapter pattern) 枚举器和迭代器 具体解释
适配器模式(adapter pattern) 枚举器和迭代器 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考适配器模式(adapter patter ...
- 【Unity|C#】基础篇(20)——枚举器与迭代器(IEnumerable/IEnumerator)
[学习资料] <C#图解教程>(第18章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu. ...
随机推荐
- ApeForms | WinForm窗体UI美化库(Metro扁平风格)演示与安装
ApeForms系列① 快速上手 @ 目录 ApeForms系列① 快速上手 前言 演示视频 快速上手 安装及使用 Demo下载 联系开发者 加入我们 建议与咨询 前言 ApeForms是一套基于Wi ...
- Flink整合面向用户的数据流SDKs/API(Flink关于弃用Dataset API的论述)
动机 Flink提供了三种主要的sdk/API来编写程序:Table API/SQL.DataStream API和DataSet API.我们认为这个API太多了,建议弃用DataSet API,而 ...
- 使用Rclone将Onedirve挂载到Linux本地
1. centos挂载onedrive时, 需要安装fuse. # 安装fuse yum -y install fuse 2. 安装完fuse后使用rclone进行挂载 #创建挂载目录 mkdir - ...
- 计算机网络 - OSI 7层网络模型各层对应的功能
应用层 - 负责给应用程序提供统一的接口 表示层 - 负责把数据的解压缩和编码 会话层 - 负责会话的管理(建立和终止) 传输层 - 负责端到端的数据传输 网络层 - 负责数据的路由.转发.分片 数据 ...
- Python中plt.plot()、plt.scatter()和plt.legend函数的用法示例
参考:http://www.cppcns.com/jiaoben/python/471948.html https://blog.csdn.net/weixin_44825185/article/de ...
- 第6章 字符串(下)——C++字符串
6.5 C++ strings(C++字符串) C风格字符串常见错误:试图去访问数组范围以外的元素:没有使用函数strcpy( )来实现字符串之间的复制:没有使用函数strcmp( )来比较两个字符串 ...
- Centos 创建新的虚拟环境
1. conda env list 查看目前已经存在的虚拟环境,注意新取的虚拟环境的名字不能和目前已存在的虚拟环境的名字相同! 2. conda create -n [环境名] [指定python版本 ...
- 【SpringBoot】快速入门
博客主页:准Java全栈开发工程师 00年出生,即将进入职场闯荡,目标赚钱,可能会有人觉得我格局小.觉得俗,但不得不承认这个世界已经不再是以一条线来分割的平面,而是围绕财富旋转的球面,成为有钱人不是为 ...
- BUUCTF-ningen
ningen 从16进制看可以发现其中有压缩包,存在着504b0304,使用binwalk分离即可 压缩包带密码,根据提示是四位纯数字 使用ARCHPR破解即可
- 【RPA之家转载RPA创新产业峰会回看】机器人流程自动化专利态势报告
[RPA之家转载RPA创新产业峰会回看]机器人流程自动化专利态势报告 自动化的一个专利情况的监测,就是全球监测的情况.今天我可能给大家汇报的主要是三个方面,第一个方面就是讲一下全球投资智能化的专利的一 ...