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 ...
随机推荐
- macOS 安装 wget
适用于macOS Sierra Apple Store下载安装Xcode 安装Homebrew包管理,类似于Ubuntu下的apt-get: 终端下输入 ruby -e "$(curl -f ...
- linux中安装eclipse,安装好之后不能直接建servlet,不能直接在jsp页面中run on server.权限在作怪,我猜的,
最近按照网上的教程安装了eclipse,开始很开心,但是慢慢的用的时候发现,不能在jsp页面run on server ,不能创建servlet,我也是醉了,我当时就猜测是权限问题,很多软件安装之后都 ...
- Jenkins配置MSBuild时使用环境变量
[MSBuild Plugin]插件在使用环境变量有个很奇葩的方式,比如我们通常在Windows的节点机器上,使用WORKSPACE环境变量时,批处理应该这样写%WORKSPACE%,而有时插件确不能 ...
- HC-05初探
catalogue . 蓝牙嗅探抓包 . HC05蓝牙模块AT模式设置 . USB转串口芯片CH340 . 蓝牙小车 1. 蓝牙嗅探抓包 针对蓝牙通信包的嗅探抓包不能直接使用wincap+wiresh ...
- mainBundle和CustomBundle
iOS中NSBundle类 An NSBundle object represents a location in the file system that groups code and resou ...
- Android应用内语言切换实现(转)
使用Java反射机制 IActivityManager与ActivityManagerNative都是非公开类,使用Java反射去调用其中的方法. 第一步.使用Android开放的api更改Confi ...
- git命令拾遗
要随时掌握工作区的状态,使用git status命令. 如果git status告诉你有文件被修改过,用git diff可以查看修改内容. HEAD指向的版本就是当前版本,因此,Git允许我们在版本的 ...
- 利用jquery实现网站中对应栏目下面内容切换效果。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Windows XP SP3 Professional 微软(MSDN)官方原版系统
Windows XP SP3 Professional 微软(MSDN)官方原版系统 Windows XP(版本号:5.1,开发代号:Whistler)是微软公司推出供个人电脑使用的操作系统,其RTM ...
- centos搭建 nginx一直报错 file not found.
百度了半天找到别人的解决办法 记录下 摘要: file not found. nginx php 这个问题是你配置文件的问题: 查看就是了不要管 nginx 如何开启解析 PHP 的功能? # 成功安 ...