c# IEnumerable和IEnumerator枚举器
学习笔记:《深入理解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枚举器的更多相关文章
- C# ~ 从 IEnumerable / IEnumerator 到 IEnumerable<T> / IEnumerator<T> 到 yield
IEnumerable / IEnumerator 首先,IEnumerable / IEnumerator 接口定义如下: public interface IEnumerable /// 可枚举接 ...
- C#图解教程 第十八章 枚举器和迭代器
枚举器和迭代器 枚举器和可枚举类型 foreach语句 IEnumerator接口 使用IEnumerable和IEnumerator的示例 泛型枚举接口迭代器 迭代器块使用迭代器来创建枚举器使用迭代 ...
- C# 枚举器和迭代器
一.枚举器(enumerator)和可枚举类型(enumeration) 我们都知道foreach语句可以用来遍历数组中的元素,但你有没有想过为什么它可以被foreach处理呢? 这是因为数组可以按需 ...
- C# 枚举器(enumerator)
总结: 1.枚举器就像是序列中的"游标"或"书签".可以有多个"书签",移动其中任何一个都可以枚举集合,与其他枚举器互不影响.用来遍历数据结 ...
- 关于IEnumerator<T>泛型枚举器 和 IEnumerable<T>
在开发中我们经常会用到 IEnumerable<T> xxx 或者 List<T> xxx 这种集合或者集合接口,实际上就是一个线性表嘛然后结合C#提供的语法糖 foreach ...
- 【Unity|C#】基础篇(20)——枚举器与迭代器(IEnumerable/IEnumerator)
[学习资料] <C#图解教程>(第18章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu. ...
- C#枚举器接口IEnumerator的实现
原文(http://blog.csdn.net/phpxin123/article/details/7897226) 在C#中,如果一个类要使用foreach结构来实现迭代,就必须实现IEnumera ...
- C#中的foreach语句与枚举器接口(IEnumerator)及其泛型 相关问题
这个问题从<C#高级编程>数组一节中的foreach语句(6.7.2)发现的. 因为示例代码与之前的章节连贯,所以我修改了一下,把自定义类型改为了int int[] bs = { 2, 3 ...
- 【C#】构建可枚举类型(IEnumerable和IEnumerator)
为了开始对实现既有接口的了解,我们就看一下IEnumerable和IEnumerator的作用,想一下,C#支持关键字foreach,允许我们遍历任何数组类型的内容: //遍历数组的项 ,,} for ...
随机推荐
- [leetcode.com]算法题目 - Plus One
Given a number represented as an array of digits, plus one to the number. class Solution { public: v ...
- Android sharedUserId 和系统权限
sharedUserId 给不同的应用使用同一个 sharedUserId 可以运行在这几个应用间互相访问数据(数据库,SharedPreferences,文件). sharedUserId 一旦使用 ...
- centos7下 vsftpd初使用
一. 安装 1. 命令: yum -y install vsftpd 2. 创建一个用户专门用来登录vsftpd #在根目录下创建一个文件夹ftpfile mkdir ftpfile #创建用户ft ...
- spring cloud学习(一) 服务注册
首先spring-cloud相关的简介可以去百度搜索,这里就不多说了,这里分享一个翻译spring cloud官网的中文网站spring cloud中文网 这个学习项目的代码放在 https://gi ...
- pika配置文件说明
# Pika 端口 port : 9221 # pika进程数量,不建议超过核心数量,pika是多线程的 thread-num : 1 # Sync 线程数量 sync-thread-num : 6 ...
- 几种int类型的范围
我们在编程的过程经常会遇到数据溢出的情况,于是这个时候我们必须定义能表示更大的数的数据类型来表示这个数. 下面列出了int型的范围: unsigned int 0-4294967295 ...
- django-suit报错解决-----from suit.apps import DjangoSuitConfig
(py27) [root@test SimpletourDevops]# python manage.py makemigrationsTraceback (most recent call last ...
- 学习推荐-Redis学习手册
redis之旅: http://www.cnblogs.com/stephen-liu74/archive/2012/02/27/2370212.html
- npm安装第三方库找不到“cl.exe”问题
1.安装第三方库时找不到"cl.exe"的解决方法 安装 本地 remix时 出现错误(npm install remix-ide -g) 原因:remix 依赖的 python库 ...
- nodejs结合apiblue实现MockServer
apiblue功能很强大,里面支持很多插件,这些插件能够为restfulAPI提供接口文档自动生成,甚至Mockserver的功能,当然,好多插件还是有很多坑的.下面用apiblue实现下面的业务需求 ...