IEnumerable 接口只包含一个抽象的方法 GetEnumerator(),它返回一个可用于循环访问集合的 IEnumerator 对象,IEnumerator 对象是一个集合访问器。

需要给自定义的类实现 foreach 功能,就需要实现 IEnumerable 接口,下面给出一个例子。

using System;
using System.Collections;
using System.Collections.Generic; class NewList<T> : IEnumerable //实现 IEnumerable 接口的 GetEnumerator()
{
private List<T> newList = new List<T>(); public void Add(T item)
{
newList.Add(item);
}
public IEnumerator GetEnumerator()
{
return this.newList.GetEnumerator();
}
} class Program
{
static void Main(string[] args)
{
NewList<string> newList = new NewList<string>();
newList.Add("a");
newList.Add("b");
foreach(string item in newList)
{
Console.WriteLine("item:" + item);
}
}
}

手工实现IEnumberable接口和IEnumerator接口中的方法实现的方式如下:

using System;
using System.Collections;
using System.Collections.Generic; class NewList<T> : IEnumerable //实现 IEnumerable 接口的 GetEnumerator()
{
private List<T> list = new List<T>(); public void Add(T item)
{
list.Add(item);
}
public IEnumerator GetEnumerator()
{
return new NewListEnumerator<T>(this);
} private class NewListEnumerator<T>: IEnumerator
{
private int position = -;
private NewList<T> newList;
public NewListEnumerator(NewList<T> newList)
{
this.newList = newList;
} public object Current
{
get
{
return newList.list[position];
}
} public bool MoveNext()
{
if(position < newList.list.Count - )
{
position++;
return true;
}
else
{
return false;
}
} public void Reset()
{
position = -;
}
}
} class Program
{
static void Main(string[] args)
{
NewList<string> newList = new NewList<string>();
newList.Add("a");
newList.Add("b");
foreach(string item in newList)
{
Console.WriteLine("item:" + item);
}
}
}

IEnumerable 和 IEnumerator的更多相关文章

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

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

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

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

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

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

  4. IEnumerable和IEnumerator

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

  5. 关于迭代器中IEnumerable与IEnumerator的区别

    首先是IEnumerable与IEnumerator的定义: 1.IEnumerable接口允许使用foreach循环,包含GetEnumerator()方法,可以迭代集合中的项. 2.IEnumer ...

  6. IEnumerable和IEnumerator 详解 (转)

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

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

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

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

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

  9. 转载IEnumerable与IEnumerator区别

    public interface IEnumerable {     IEnumerator GetEnumerator(); }   public interface IEnumerator {   ...

  10. IEnumerable、IEnumerator与yield的学习

    我们知道数组对象可以使用foreach迭代进行遍历,同时我们发现类ArrayList和List也可以使用foreach进行迭代.如果我们自己编写的类也需要使用foreach进行迭代时该怎么办呢? IE ...

随机推荐

  1. 【uoj262】 NOIP2016—换教室

    http://uoj.ac/problem/262 (题目链接) 题意 有${n}$个时间段,第${i}$个时间段可以选择在${c_i}$教室上课,也可以选择申请换课,有${k_i}$概率申请通过,在 ...

  2. RAC 相关概念解释

    1.1 并发控制 在集群环境中, 关键数据通常是共享存放的,比如放在共享磁盘上. 而各个节点的对数据有相同的访问权限, 这时就必须有某种机制能够控制节点对数据的访问. Oracle RAC 是利用DL ...

  3. android布局学习-使用FrameLayout和LinearLayout制作QQ空间底部导航栏

    [声明:本博客通过学习“J灬叶小超 ”博客而写,链接:http://www.cnblogs.com/yc-755909659/p/4288260.html] --------------------- ...

  4. Android登录界面实现

    花了一些时间实现了一个还算可以等登陆界面,主要是对这两天工作的一个总结:自定义按钮.编辑框.布局.全屏等. 效果如下: 获取代码:点这里

  5. CentOS加载U盘

    概述: 把CentOS设置成了启动进入命令行,结果不知道在哪儿找U盘了,于是搜集了一些命令. 1. 查看分区信息,以确定那个是U盘 使用root执行fdisk -l,确定U盘是sdb1 2. 挂载U盘 ...

  6. 无法启动此程序,因为计算机中丢失MSVCP110.dll

    安装Visual C++ Redistributable for Visual Studio 2012 有arm.x86.x64有三个版本. 如果应用程序为debug版本而不是release版本,可能 ...

  7. springMVC的注解@RequestParam与@PathVariable的区别

    1.在SpringMVC后台控制层获取参数的方式主要有两种, 一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取. ...

  8. Jquery 基本知识(一)

    1. DOM对象:通过例如getElementById方法获取到DOM树中的元素就是DOM对象 jQuery对象:通过jQuery包装DOM对象后产生的对象 --- 注意:jQuery对象和DOM对象 ...

  9. join的理解

    thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程.比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B. t.join( ...

  10. 转:遗传算法解决TSP问题

    1.编码 这篇文章中遗传算法对TSP问题的解空间编码是十进制编码.如果有十个城市,编码可以如下: 0 1 2 3 4 5 6 7 8 9 这条编码代表着一条路径,先经过0,再经过1,依次下去. 2.选 ...