IEnumerable 和 IEnumerator
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的更多相关文章
- 细说 C# 中的 IEnumerable和IEnumerator接口
我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq to Object中要返回IEnumerable? 接下来,先开始我们的正 ...
- 迭代器学习之一:使用IEnumerable和IEnumerator接口
写博客是检验我学习的成果之一以及自我总结的一种方式,以后会经常利用这种方式进行技术交流和自我总结,其中认识不深难免会有错误,但是一直懂得不懂就问,不懂就学的道理! 1.首先看一个简单的列子 , , , ...
- C# ~ 从 IEnumerable / IEnumerator 到 IEnumerable<T> / IEnumerator<T> 到 yield
IEnumerable / IEnumerator 首先,IEnumerable / IEnumerator 接口定义如下: public interface IEnumerable /// 可枚举接 ...
- IEnumerable和IEnumerator
概述 IEnumerable和IEnumerator接口存在的意义:用来实现迭代的功能! public interface IEnumerable { IEnumerator GetEnumerato ...
- 关于迭代器中IEnumerable与IEnumerator的区别
首先是IEnumerable与IEnumerator的定义: 1.IEnumerable接口允许使用foreach循环,包含GetEnumerator()方法,可以迭代集合中的项. 2.IEnumer ...
- IEnumerable和IEnumerator 详解 (转)
原文链接:http://blog.csdn.net/byondocean/article/details/6871881 参考链接:http://www.cnblogs.com/hsapphire/a ...
- C#基础知识系列九(对IEnumerable和IEnumerator接口的糊涂认识)
前言 IEnumerable.IEnumerator到现在为止对这两个接口还是不太理解,不理解但是自己总是想着试着要搞明白,毕竟自己用的少,所以在此先记录一下.以备自己日后可以来翻查,同时也希望园子里 ...
- [转]那些年我还不懂:IList,ICollection,IEnumerable,IEnumerator,IQueryable
1.首先看一个简单的例子 int[] myArray = { 1, 32, 43, 343 }; IEnumerator myie = myArray.GetEnumerator(); myie.Re ...
- 转载IEnumerable与IEnumerator区别
public interface IEnumerable { IEnumerator GetEnumerator(); } public interface IEnumerator { ...
- IEnumerable、IEnumerator与yield的学习
我们知道数组对象可以使用foreach迭代进行遍历,同时我们发现类ArrayList和List也可以使用foreach进行迭代.如果我们自己编写的类也需要使用foreach进行迭代时该怎么办呢? IE ...
随机推荐
- win10 1607 密匙
win10 1607 安装密钥 GVLK Core=YTMG3-N6DKC-DKB77-7M9GH-8HVX7 Professional=VK7JG-NPHTM-C97JM-9MPGT-3V66T E ...
- UVa1636 Headshot
数据结构学得心累,做点小题换心情 原题是PDF格式查看的,贴过来好麻烦,果断放弃 已知前一次尝试结果是0,则可以得知: 下一次若仍是0,则遇到了一个00串 下一次若是1,则遇到了一个01串 SHOOT ...
- C/C++ Learning
目录 1. C/C++中的关键字2. C/C++中的标识符3. 编译选项MD(d).MT(d)编译选项的区别4. C++类模板.函数模板5. C++修饰符6. 调用约定7. 错误处理8. 环境表 9. ...
- pom中定义某jar包的依赖,但并不使用该jar包,那最后部署的应用中会有这个jar包么?
关于这个问题,首先得明确这个jar包的依赖是怎么定义的,我们知道在maven的pom文件中,会有:dependencymanagement和dependency2个部分 一般我们说在depende ...
- [Android] HttpURLConnection & HttpClient & Socket
Android的三种网络联接方式 1.标准Java接口:java.net.*提供相关的类//定义地址URL url = new URL("http://www.google.com" ...
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-13: ordinal not i│ n range(128)
python保持网页文件遇到的错误,归根结底还是编码问题,改一下要保存的数据为utf-8就好了. 如下最简单: import sys reload(sys) sys.setdefaultencodin ...
- WPF控件ComboBox 每个Item的ToolTip引发的异常
介绍 首先介绍下要实现的任务.做一个下拉框,当选择每个项的时候将鼠标发在上面显示该项的ToolTip的内容(Image). 实现 Model: public class SkinInfo : Noti ...
- Chrome控制台 JS调试的一些小技巧
$ $_命令返回最近一次表达式执行的结果,功能跟按向上的方向键再回车是一样的,但它可以做为一个变量使用在你接下来的表达式中. $0~$4则代表了最近5个你选择过的DOM节点.在页面右击选择审查元素,然 ...
- JavaWeb---总结(九)通过Servlet生成验证码图片
一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下: 创建一个DrawImage Servlet,用来生成验证码图片 1 package gacl. ...
- MSDeploy 同步时不删除原有文件
在 jenkins里 Execute Windows batch command "C:\Program Files (x86)\IIS\Microsoft Web Deploy V3\m ...