学习笔记:《深入理解C#》第六章:实现迭代器的捷径

1:C#1:手写迭代器的痛苦

迭代器的模式重要方面就是,不用一次返回所有数据,调用代码一次只需要获取一个元素。

迭代器的内部实现原理:

public class Person
{
public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
} public string firstName;
public string lastName;
} public class People : IEnumerable
{
private Person[] _people;
public People(Person[] pArray)
{
_people = new Person[pArray.Length]; for (int i = ; i < pArray.Length; i++)
{
_people[i] = pArray[i];
}
} public IEnumerator GetEnumerator()
{
for (int i = ; i < _people.Length; i++)
{
yield return _people[i];
}
}
} public class PeopleEnum : IEnumerator
{
public Person[] _people; int position = -; public PeopleEnum(Person[] list)
{
_people = list;
} public bool MoveNext()
{
position++;
return (position < _people.Length);
} public void Reset()
{
position = -;
} object IEnumerator.Current
{
get
{
return Current;
}
} public Person Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
class Program
{
static void Main(string[] args)
{
Person[] peopleArray = new Person[]
{
new Person("John", "Smith"),
new Person("Jim", "Johnson"),
new Person("Sue", "Rabon"),
}; People peopleList = new People(peopleArray); foreach (Person p in peopleList)
Console.WriteLine(p.firstName + " " + p.lastName); Console.ReadKey();
}
}

2:c#2:利用yield语句简化迭代器

将Person类修改如下

public class People : IEnumerable
{
private Person[] _people;
public People(Person[] pArray)
{
_people = new Person[pArray.Length]; for (int i = ; i < pArray.Length; i++)
{
_people[i] = pArray[i];
}
} public IEnumerator GetEnumerator()
{
for (int i = ; i < _people.Length; i++)
{
yield return _people[i];
}
}
}

3:实例:利用IEnumerable<T>实现计算斐波那契数列

如果不想预先进行计算,并把结果缓存起来供后来使用那么下面方法是非常有用的,他会计算出一个斐波那契数列,数量有输入参数指定,延后把结果封装到IEnumerable中,以便foreach或以IEnumerable为输入的方法使用。

class Program
{
static void Main(string[] args)
{
foreach (long fib in FibonacciGenerator.GetSequence())
{
Console.WriteLine(fib);
} Console.ReadKey();
}
} class FibonacciGenerator
{
public static IEnumerable<long> GetSequence(int count)
{
long fib1 = ;
long fib2 = ;
yield return fib1;
yield return fib2; while (--count != )
{
long fib3 = fib1 + fib2;
yield return fib3;
fib1 = fib2;
fib2 = fib3;
}
}
}
}

c# IEnumerable和IEnumerator枚举器的更多相关文章

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

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

  2. C#图解教程 第十八章 枚举器和迭代器

    枚举器和迭代器 枚举器和可枚举类型 foreach语句 IEnumerator接口 使用IEnumerable和IEnumerator的示例 泛型枚举接口迭代器 迭代器块使用迭代器来创建枚举器使用迭代 ...

  3. C# 枚举器和迭代器

    一.枚举器(enumerator)和可枚举类型(enumeration) 我们都知道foreach语句可以用来遍历数组中的元素,但你有没有想过为什么它可以被foreach处理呢? 这是因为数组可以按需 ...

  4. C# 枚举器(enumerator)

    总结: 1.枚举器就像是序列中的"游标"或"书签".可以有多个"书签",移动其中任何一个都可以枚举集合,与其他枚举器互不影响.用来遍历数据结 ...

  5. 关于IEnumerator<T>泛型枚举器 和 IEnumerable<T>

    在开发中我们经常会用到 IEnumerable<T> xxx 或者 List<T> xxx 这种集合或者集合接口,实际上就是一个线性表嘛然后结合C#提供的语法糖 foreach ...

  6. 【Unity|C#】基础篇(20)——枚举器与迭代器(IEnumerable/IEnumerator)

    [学习资料] <C#图解教程>(第18章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu. ...

  7. C#枚举器接口IEnumerator的实现

    原文(http://blog.csdn.net/phpxin123/article/details/7897226) 在C#中,如果一个类要使用foreach结构来实现迭代,就必须实现IEnumera ...

  8. C#中的foreach语句与枚举器接口(IEnumerator)及其泛型 相关问题

    这个问题从<C#高级编程>数组一节中的foreach语句(6.7.2)发现的. 因为示例代码与之前的章节连贯,所以我修改了一下,把自定义类型改为了int int[] bs = { 2, 3 ...

  9. 【C#】构建可枚举类型(IEnumerable和IEnumerator)

    为了开始对实现既有接口的了解,我们就看一下IEnumerable和IEnumerator的作用,想一下,C#支持关键字foreach,允许我们遍历任何数组类型的内容: //遍历数组的项 ,,} for ...

随机推荐

  1. 通过javascript 直接播放amr格式的语言

    前段时间做了个功能(有2.3个月了,突然想起来了,就记录一下),语言播放.一开始觉得很简单~~~ 计划应用的是H5的audio标签,但因为这个标签不支持amr格式的语言,但是手机端传到后台的录音却都是 ...

  2. JQuery Mobile - 处理图片加载失败!

    重点来了:一定要记住error事件不冒泡(如果要用js的方法替换默认出错图片,记得把img的alt属性去掉). 相关的知识点:jquery的ready方法.$("img").err ...

  3. HDU - 2604 Queuing(递推式+矩阵快速幂)

    Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  4. 【Anaconda】:科学计算的Python发行版

    [背景] Python易用,但包管理和Python不同版本的问题比较头疼,特别是当你使用Windows的时候.为了解决这些问题,有不少发行版的Python,比如WinPython.Anaconda等, ...

  5. CSP攻略

    看完三篇文章应该就懂了csp是干嘛的. https://www.cnblogs.com/Wayou/p/intro_to_content_security_policy.html https://ww ...

  6. 7. Bagging & Random Forest

    通过前面集成学习的介绍我们知道,欲得到泛化性能强的集成学习器,集成中个体学习器应尽量相互独立:虽然“独立”在现实任务中无法做到,但可以设法使基学习器尽可能具有较大差异. 1. Bagging 自助采样 ...

  7. 并发上下文控制包Context

    Context,是golang用来控制并发流程的库,它能方便的将主控程序的停止信号传递到goroutinue中,从而实现一键中止关联goroutinue的执行,除此之外,它还能将外部变量通过Value ...

  8. 【xsy1281】 珠串 打表+乱搞or数位dp

    题目大意:你要找出一个有$k$个的本质不同的$n$位二进制数的集合,使得集合中最大的数最小,请输出这个数 本质不同定义:对于一个数$k$,$rev(k)$,$~k$,$rev(~k)$与$k$本质相同 ...

  9. vue教程3-07 vue-loader

    vue-loader: vue-loader: 其他loader -> css-loader.url-loader.html-loader..... 后台: nodeJs -> requi ...

  10. 分享:基于Dracula+Zenburn 自定制的pycharm主题配色文件

    显示效果: PS:彩色配色,一是可以提高平时写代码的乐趣,另一个是,对视力相对比较好. 配置方法: 1. 在你本地的操作系统里,找到pycharm安装的时候,默认在C盘创建的文件夹colors:C:\ ...