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. ...
随机推荐
- 【雅礼集训 2017 Day2】棋盘游戏
loj 6033 description 给一个\(n*m\)的棋盘,'.'为可通行,'#'为障碍.Alice选择一个起始点,Bob先手从该点往四个方向走一步,Alice再走,不能走走过的点,谁不能动 ...
- 差分优化建边(Tax)
[Luogu P6822PA2012]Tax] (http://www.luogu.com.cn/problem/P6822") All right. Let's go! 题目描述 给出一个 ...
- 多态——JavaSE基础
多态 同一个方法可以根据对象的不同采取不同的动作 一个对象的实际类型是确定的,但可以指向对象的引用类型有很多 基本条件: 有继承关系 子类重写父类方法 父类引用指向子类对象Father f1 = ne ...
- java中的final与可变类型、不可变类型的关系
如果你对final和不可变类型的概念与区别有疑问的话,可以打开这篇文章.希望我的解答可以帮到您! 1.不可变类型: 什么是可变类型,什么是不可变类型呢? 首先我们看一下下面的这行代码: String ...
- iPhone x 的区别
最近入手两台iPhone x, 均从官网购买,两台分别是2017年和2018年生产,对比了一下,两台还有是一些差别: 首先苹果X使用起来还是非常爽的,没有HOME键,明显比按HOME键方便,因为按HO ...
- C语言学习之我见-strncpy()字符串复制函数(可控制范围)
strncpy()函数,用于两个字符串值的复制. (1)函数原型 char *strncpy(char * _Dest,const char * _Source,size_t _Count); (2) ...
- MySQL-5-TCL,视图,变量,存储过程和函数,流程控制
TCL:Transaction Control Language事务控制语言 TCL 事务的特点 acid: 原子性(Atomicity),一致性(Consistency),隔离性(isolation ...
- webpack中文api
1. 简介 1.Plugins://插件 webpack has a rich plugin interface.Most of the features are internal plug ...
- 数组基础篇(对应C++ Primer plus 4.10)
概要:数组是由一组同类型的元素组成的集合,在内存上是一片连续的存储空间.C++提供了三种数组的表示方法:普通数组,模板类vector(C++98 新增的标准模板库STL提供该模板类)和模板类array ...
- adb工具
ADB:全称为Android Debug Bridge,它是 Android 开发/测试人员不可替代的强大工具. 首先,下载ADB工具并安装: 下载:百度就有.下载后是个压缩包,将其拷贝到cm ...