学习笔记:《深入理解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. this与$(this)对象

    this与$(this)对象.前者是Javascript对象,而后者是jQuery是对象.两者分清楚,它们只能使用自己的方法.Javascript对象使用Javascript的方法,jQuery对象使 ...

  2. 百度小程序button去掉默认边框

    百度小程序button去掉默认边框: button::after{ border:none; }

  3. [学习笔记]BSGS

    \(\%\%\% Fading\) 早就会了,我最近才理解,当时颓废太多忘学了 1.[SDOI2013]随机数生成器 当天正好在学数列,回来发现用必修五的知识就没了-- 不过特判好烦啊. \(Code ...

  4. 学习人工智还死拽着Python不放?大牛都在用Anaconda5.2.0

    前言 最近有很多的小白想学习人工智能,可是呢?依旧用Python在学习.我说大哥们,现在都什么年代了,还在把那个当宝一样拽着死死不放吗?懂的人都在用Anaconda5.2.0,里面的功能可强大多了,里 ...

  5. 协程 coroutine

    参考链接: http://manual.luaer.cn/2.11.html http://www.cnblogs.com/riceball/archive/2008/01/03/1025158.ht ...

  6. GoLang学习之数据类型

    数据类型 Go语言按类别有以下几种数据类型: bool,一个字节,值是true或者false,不可以用0或者1表示 int/uint(带符号为与不带符号位的int类型):根据平台不同是32位或者64位 ...

  7. python学习笔记14-函数

    使用关键字def来创建函数  注意缩进 函数命名规则: 1.必须以下划线或者字母开头 2.区分大小写 3.不能是保留字 调用函数一定记得加括号 def print_info(name,age) pri ...

  8. webpack 打包调试

    本文适用于已经会使用webpack的前端开发人员,但是想进一步了解webpack细节和进阶. 首先请读者按照我前一篇文章 Webpack 10分钟入门介绍的步骤,在本地搭建一个webpack的hell ...

  9. Win7删除网络位置那些不用的网络位置(驱动器)

    1.初始状态: 映射成功的网络位置如下图 2.要删除这个网络位置:点击"打开网络和共享中心",然后如下图设置: 3.重启电脑之后,删除的"网络位置"不会在资源管 ...

  10. IntelliJ IDEA导入多个eclipse项目到同一个workspace下

    IntelliJ IDEA 与eclipse在新建项目上工作区的叫法略有不同,区别见下图. 我们在eclipse都是在新建的workspace目录下新建我们的项目,但是在IDEA中没有workspac ...