我们先思考几个问题:
1.为什么在foreach中不能修改item的值?(IEnumerator的Current为只读)
2.要实现foreach需要满足什么条件?(实现IEnumerator接口来实现的)
3.为什么Linq to Object中要返回IEnumerable?(因为IEnumerable是延迟加载的,每次访问的时候才取值。也就是我们在Lambda里面写的where、select并没有循环遍历(只是在组装条件),只有在ToList或foreache的时候才真正去集合取值了。这样大大提高了性能。)

.net中迭代器是通过IEnumerable和IEnumerator接口来实现的,今天我们也来依葫芦画瓢。

using System;
using System.Collections; namespace RedisTest
{
class Program
{
static void Main(string[] args)
{
string[] str = { "", "", "", "", "" };
var aaa = new MyIEnumerable(str);
var bbb = aaa.GetEnumerator();
while (bbb.MoveNext())
{
Console.WriteLine(bbb.Current);
}
Console.WriteLine("---------------------------------------------------------");
foreach (var item in aaa)
{
Console.WriteLine(item);
}
Console.Read();
/*
1111
2222
3333
4444
5555
---------------------------------------------------------
1111
2222
3333
4444
5555
*/
}
} public class MyIEnumerable : IEnumerable
{
private string[] strList;
public MyIEnumerable(string[] _strList)
{
strList = _strList;
}
public IEnumerator GetEnumerator()
{
return new MyIEnumerator(strList);
}
} public class MyIEnumerator : IEnumerator
{
private string[] strList;
private int position; public MyIEnumerator(string[] _strList)
{
strList = _strList;
position = -;
}
public object Current
{
get
{
return strList[position];
}
} public bool MoveNext()
{
position++;
if (position < strList.Length)
return true;
return false;
} public void Reset()
{
position = -;
}
}
}

yield的使用

using System;
using System.Collections; namespace RedisTest
{
class Program
{
static void Main(string[] args)
{
string[] str = { "", "", "", "", "" };
var aaa = new MyIEnumerable(str);
var bbb = aaa.GetEnumerator();
while (bbb.MoveNext())
{
Console.WriteLine(bbb.Current);
}
Console.WriteLine("---------------------------------------------------------");
foreach (var item in aaa)
{
Console.WriteLine(item);
}
Console.Read();
/*
1111
2222
3333
4444
5555
---------------------------------------------------------
1111
2222
3333
4444
5555
*/ }
} public class MyIEnumerable
{
private string[] strList;
public MyIEnumerable(string[] _strList)
{
strList = _strList;
}
public IEnumerator GetEnumerator()
{
for (int i = ; i < strList.Length; i++)
{
yield return strList[i];
}
}
} }

我们调用GetEnumerator的时候,看似里面for循环了一次,其实这个时候没有做任何操作。只有调用MoveNext的时候才会对应调用for循环:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq; namespace RedisTest
{
class Program
{
static void Main(string[] args)
{
string[] str = { "", "", "", "", "" };
var aaa = new MyIEnumerable(str);
var bbb = aaa.MyWhere(x => x != "");
var ccc = bbb.ToList();
//现在看到了吧。执行到MyWhere的时候什么动作都没有(返回的就是IEnumerable),只有执行到ToList的时候才代码才真正的去遍历筛选。
//这里的MyWhere其实可以用扩展方法来实现,提升逼格。(Linq的那些查询操作符就是以扩展的形式实现的)
Console.Read(); }
} public class MyIEnumerable
{
private string[] strList;
public MyIEnumerable(string[] _strList)
{
strList = _strList;
}
public IEnumerator GetEnumerator()
{
for (int i = ; i < strList.Length; i++)
{
yield return strList[i];
}
}
public IEnumerable<string> MyWhere(Func<string, bool> func)
{
foreach (string item in this)
{
if (func(item))
{
yield return item;
}
}
}
} }

IEnumerable和IEnumerator接口的更多相关文章

  1. 细说 C# 中的 IEnumerable和IEnumerator接口

    我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq to Object中要返回IEnumerable? 接下来,先开始我们的正 ...

  2. C# IEnumerable 和 IEnumerator接口浅析

    温故而知新,可以为师矣,有空经常复习一下基础知识是有必要的,并且能加深理解和记忆. Foreach常用于循环访问集合,对实现IEnumerable的接口的容器进行遍历,IEnumerable和IEnu ...

  3. IEnumerable、IEnumerator接口(如何增加迭代器功能)

    IEnumerable.IEnumerator接口封装了迭代器功能,有了它,我们不需要将内部集合暴露出去,外界只需要访问我的迭代器接口方法即可遍历数据. 在C#中,使用foreach语句来遍历集合.f ...

  4. 迭代器学习之一:使用IEnumerable和IEnumerator接口

    写博客是检验我学习的成果之一以及自我总结的一种方式,以后会经常利用这种方式进行技术交流和自我总结,其中认识不深难免会有错误,但是一直懂得不懂就问,不懂就学的道理! 1.首先看一个简单的列子 , , , ...

  5. C#基础知识系列九(对IEnumerable和IEnumerator接口的糊涂认识)

    前言 IEnumerable.IEnumerator到现在为止对这两个接口还是不太理解,不理解但是自己总是想着试着要搞明白,毕竟自己用的少,所以在此先记录一下.以备自己日后可以来翻查,同时也希望园子里 ...

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

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

  7. IEnumerable和IEnumerator

    概述 IEnumerable和IEnumerator接口存在的意义:用来实现迭代的功能! public interface IEnumerable { IEnumerator GetEnumerato ...

  8. IEnumerable和IEnumerator 详解 (转)

    原文链接:http://blog.csdn.net/byondocean/article/details/6871881 参考链接:http://www.cnblogs.com/hsapphire/a ...

  9. [转]那些年我还不懂:IList,ICollection,IEnumerable,IEnumerator,IQueryable

    1.首先看一个简单的例子 int[] myArray = { 1, 32, 43, 343 }; IEnumerator myie = myArray.GetEnumerator(); myie.Re ...

随机推荐

  1. UVA 10382 Watering Grass(区间覆盖)

    n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...

  2. C# CEF 封装UserControl

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; u ...

  3. Jquery 获取radio选中值

  4. Docker入门03——Container

    1 启动容器 1.1 新建并启动 1.2 启动已终止容器 2 后台运行 3 终止 4 进入容器 5 导入和导出 5.1 导出 5.2 导入 6 删除 1 启动容器 1.1 新建并启动 docker r ...

  5. JavaScript之函数式编程思想初探

    //result = 3*x + 5; var Mul3 = function(x){ return 3*x; } var Add5 = function(x){ return x + 5; } va ...

  6. luogu P2779 [AHOI2016初中组]黑白序列

    传送门 注:本题解中下标从1开始 这题可以想出一个\(O(n^2)\)的dp,只要考虑每个偶数位置可以从前面的哪个位置加上一个"B...W..."转移过来 然而数据范围有5e5,, ...

  7. POJ2031 Building a Space Station【最小生成树】

    题意: 就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能够相互连通.如果两个球有重叠的部分则算为已连通,无需再搭桥.求搭建通路的最小边长总和是多少. 思路: 先处理空间点之间的距离 ...

  8. 使用Sphinx生成本地的Python帮助文档

    第一步:安装Sphinx 首先我们需要安装Sphinx,如果已经安装了Anaconda,那么只需要使用如下命令即可安装,关于其中的参数 -c anaconda,可以在链接[1]中查看: conda i ...

  9. django学习~forms

    一 简介 今天咱们来聊聊 django强大的表单功能二 Froms作用   1 自动生成HTML表单元素   2 检查表单数据的合法性   3 如果验证错误,重新显示表单(数据不会重置)   4 数据 ...

  10. systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起。

    1.centos 检查服务是否开机自启  (ntpd是原生的服务,mysql是注册的服务) 参考:1.http://man.linuxde.net/systemctl