IEnumerable & IEnumerator
- using System;
- using System.Collections;
- // Simple business object.
- public class Person
- {
- public Person(string fName, string lName)
- {
- this.firstName = fName;
- this.lastName = lName;
- }
- public string firstName;
- public string lastName;
- }
- // Collection of Person objects. This class
- // implements IEnumerable so that it can be used
- // with ForEach syntax.
- public class People : IEnumerable
- {
- private Person[] _people;
- public People(Person[] pArray)
- {
- _people = new Person[pArray.Length];
- for (int i = 0; i < pArray.Length; i++)
- {
- _people[i] = pArray[i];
- }
- }
- // Implementation for the GetEnumerator method.
- IEnumerator IEnumerable.GetEnumerator()
- {
- return (IEnumerator) GetEnumerator();
- }
- public PeopleEnum GetEnumerator()
- {
- return new PeopleEnum(_people);
- }
- }
- // When you implement IEnumerable, you must also implement IEnumerator.
- public class PeopleEnum : IEnumerator
- {
- public Person[] _people;
- // Enumerators are positioned before the first element
- // until the first MoveNext() call.
- int position = -1;
- public PeopleEnum(Person[] list)
- {
- _people = list;
- }
- public bool MoveNext()
- {
- position++;
- return (position < _people.Length);
- }
- public void Reset()
- {
- position = -1;
- }
- object IEnumerator.Current
- {
- get
- {
- return Current;
- }
- }
- public Person Current
- {
- get
- {
- try
- {
- return _people[position];
- }
- catch (IndexOutOfRangeException)
- {
- throw new InvalidOperationException();
- }
- }
- }
- }
- class App
- {
- static void Main()
- {
- Person[] peopleArray = new Person[3]
- {
- new Person("John", "Smith"),
- new Person("Jim", "Johnson"),
- new Person("Sue", "Rabon"),
- };
- People peopleList = new People(peopleArray);
- foreach (Person p in peopleList)
- Console.WriteLine(p.firstName + " " + p.lastName);
- }
- }
- /* This code produces output similar to the following:
- *
- * John Smith
- * Jim Johnson
- * Sue Rabon
- *
- */
IEnumerable & IEnumerator的更多相关文章
- C# ~ 从 IEnumerable / IEnumerator 到 IEnumerable<T> / IEnumerator<T> 到 yield
IEnumerable / IEnumerator 首先,IEnumerable / IEnumerator 接口定义如下: public interface IEnumerable /// 可枚举接 ...
- IEnumerable, IEnumerator接口
IEnumerable接口 // Exposes the enumerator, which supports a simple iteration over a non-generic collec ...
- ICollection IEnumerable/IEnumerator IDictionaryEnumerator yield
Enumerable和IEnumerator接口是.NET中非常重要的接口,二者区别: 1. IEnumerable是个声明式的接口,声明实现该接口的类就是“可迭代的enumerable”,但并没用说 ...
- c#yield,IEnumerable,IEnumerator
foreach 在编译成IL后,实际代码如下: 即:foreach实际上是先调用可枚举对象的GetEnumerator方法,得到一个Enumerator对象,然后对Enumerator进行while循 ...
- 【Unity|C#】基础篇(20)——枚举器与迭代器(IEnumerable/IEnumerator)
[学习资料] <C#图解教程>(第18章):https://www.cnblogs.com/moonache/p/7687551.html 电子书下载:https://pan.baidu. ...
- IEnumerable和IEnumerator
概述 IEnumerable和IEnumerator接口存在的意义:用来实现迭代的功能! public interface IEnumerable { IEnumerator GetEnumerato ...
- IEnumerable和IEnumerator 详解 (转)
原文链接:http://blog.csdn.net/byondocean/article/details/6871881 参考链接:http://www.cnblogs.com/hsapphire/a ...
- IEnumerator/IEnumerable接口
IEnumberator函数成员 Current返回序列中当前位置项的 属性 只读属性 返回object类型 MoveNext把枚举器位置前进到集合中下一项的方法 新位置有效返回true,否则fals ...
- 转载IEnumerable与IEnumerator区别
public interface IEnumerable { IEnumerator GetEnumerator(); } public interface IEnumerator { ...
随机推荐
- VisualSVN 5.1.7破译License Key
前面手敲一些简要的软件说明:visualSVN server大家都不陌生,服务器上的版本控制系统,一般配套Tortoisesvn(小乌龟)使用.本次介绍的这个visualsvn属于VisualStud ...
- redis 对象
redis通过前面几篇的数据结构构键了一个对象系统,这个对象系统包含了字符串对象,列表对象,哈希对象,集合对象,有序集合对象 每一个对象都是一个redisobject typedef struct r ...
- workerman例子无法工作
现象 workerman已经正常启动,但是按照官网写的例子或者下载的demo无法工作,例如页面打不开,socket连接失败等 解决方法 一般这种workerman启动没报错,但是无法打开页面或者无法连 ...
- strval
将变量转成字符串类型. 语法: string strval(mixed var); 返回值: 字符串 函数种类: PHP 系统功能 内容说明 本函数可将数组及类之外的变量类型转换成字符串类型. ...
- php对数组中的键与值进行合并处理
$res=array(); $re=array_count_values($month); foreach( $re as $k=>$v){ $arr['month_name'] = strva ...
- 给linux虚拟机添加Samba用户
Window系统连上我们的开发机Linux,自然需要在Samba里添加一个新用户. linux-06bq:/usr/local/services/samba/bin # ./smbpasswd -a ...
- Example015实现html中checkbox的全选和反选(2)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 第二章(jQuery选择器)
2.1jQuery选择器是什么 1.CSS选择器 选择器 示例 选择器 示例 标签选择器 a{ } p{ } ul{ } ID选择器 #ID{ } 类选择器 .class{ } 群组选择器 td,p, ...
- Egret的项目结构
这是我新建的一个Egret EUI项目 .wing文件夹是项目的配置文件 bin-debug 文件夹,项目编译和运行的debug目录 libs 文件夹,存放库文件,包括 Egret 核心库,其他扩展库 ...
- poj 1948 Triangular Pastures 小结
Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The o ...