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 ...
随机推荐
- CodeForces 37E Trial for Chief
Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Description Having ...
- PHP Opcode内核实现 - [ PHP内核学习 ]
catalogue . Opcode简介 . PHP中的Opcode . opcode翻译执行(即时解释执行) 1. Opcode简介 opcode是计算机指令中的一部分,用于指定要执行的操作, 指令 ...
- ApsCMS AspCms_SettingFun.asp、AspCms-qqkfFun.asp、AspCms_Slide.asp、AspCms_StyleFun.asp、login.asp、AspCms_CommonFun.asp Vul
catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 AspCMS管理系统有较多漏洞,涉及到SQL注入.密码泄漏.后台写SHE ...
- Spring MVC过滤器-委派过滤器代理(DelegatingFilterProxy)
org.springframework.web.filter中有一个特殊的类——DelegatingFilterProxy,该类其实并不能说是一个过滤器,它的原型是FilterToBeanProxy, ...
- EF-CodeFirst-2玩的嗨
时间戳.复杂类型.GUID自增长 GUID自增长 GUID用于当主建那是好处多多,但是和int不同.EF不会自动识别第一个为类名+Id开头或int类型字段 去设置自增长.尴尬的GUID怎么玩呢.. D ...
- Npoi实现Excel绘制功能
使用Npoi操作Excel,是我一直很喜欢的一种方式. 说简单也简单,但是封装好重用性,易用性,也稍稍费了些时间.在这里做个记录,免得以后遗忘. 首先说一下需求,需求有两点 1.新建Excel,并下载 ...
- 个人作业—Week1
针对教材内容的问题 阅读教材<软件工程——实践者的研究方法>Roger S.Pressman 在笼统地阅读了教材,大致理清教材知识结构后,提出以下问题作为今后学习地重点: 1) 什 ...
- uC/OS-II标志(flag)块
/*************************************************************************************************** ...
- JavaWeb学习笔记——Tomcat数据源
server.xml配置数据帐号和密码等
- Java排序算法——插入排序
import java.util.Arrays; //================================================= // File Name : Select_S ...