using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace IENumerable_Test
{ public class Person
{
public string firstName;
public string lastName; public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
}
} public class People : IEnumerable
{
private Person[] _people; public People(Person[] pArray)
{
this._people = 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();
}
//IEnumerator IEnumerable.GetEnumerator()
// {
// throw new NotImplementedException();
// } public PeopleEnum GetEnumerator()
{
return new PeopleEnum(_people);
}
} 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)
{
this._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 Program
{
static void Main(string[] args)
{
Person[] peopleArray = { 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);
Console.ReadKey();
}
}
}

  

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("*********Fun with IEnumberable/IEnumerator************\n");
Garage carLot = new Garage(); //交出集合中的每一Car对象吗
foreach (Car c in carLot)
{
Console.WriteLine("{0} is going {1} MPH", c.Name, c.Speed);
} Console.WriteLine("GetEnumerator被定义为公开的,对象用户可以与IEnumerator类型交互,下面的结果与上面是一致的"); IEnumerator i = carLot.GetEnumerator();
while(i.MoveNext())
{
//i.current返回值类型是object的
Car myCar = (Car)i.Current;
Console.WriteLine("{0} is going {1} MPH", myCar.Name, myCar.Speed);
} Console.ReadLine();
}
} public class Garage : IEnumerable
{
Car[] carArray = new Car[4]; //在Garage中定义一个Car类型的数组carArray,其实carArray在这里的本质是一个数组字段 //启动时填充一些Car对象
public Garage()
{
//为数组字段赋值
carArray[0] = new Car("Rusty", 30);
carArray[1] = new Car("Clunker", 50);
carArray[2] = new Car("Zippy", 30);
carArray[3] = new Car("Fred", 45);
} public IEnumerator GetEnumerator()
{
//递归调用
return this.carArray.GetEnumerator();
} } public class Car
{
public string Name { get; set; }
public int Speed { get; set; } public Car(string name, int speed)
{
this.Name = name;
this.Speed = speed;
}
}
}

  

IENumerable_Test的更多相关文章

随机推荐

  1. Python全栈开发:Javascript

    JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. 一.如何编写 1.J ...

  2. vue App项目 首页

    1. 下载 npm i mint-ui -S 2. 引入 import MintUI from 'mint-ui' import 'mint-ui/lib/style.css' Vue.use(Min ...

  3. SpringMVC 拦截器原理

    前言 SpringMVC 拦截器也是Aop(面向切面)思想构建,但不是 Spring Aop 动态代理实现的, 主要采用责任链和适配器的设计模式来实现,直接嵌入到 SpringMVC 入口代码里面. ...

  4. Django -- 分页 -- 批量导入

    Django --  分页 分页 Django提供了一些类实现管理数据分页,这些类位于django/core/paginator.py中 Paginator对象 Paginator(列表,int):返 ...

  5. bzoj1005题解

    [解题思路] 引理:Prufer编码 定义:不断删除树中度数为1的最小序号的点,并输出与其相连的节点的序号,直至树中只有两个节点,所得输出序列即为Prufer编码. 性质:任意一棵n节点的树都可以用长 ...

  6. 微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateLibraryList

    ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateLibraryList 1.返回顶部 1. templateMessage.getTem ...

  7. LeetCode刷题笔记-回溯法-分割回文串

    题目描述: 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab"输出:[ ["aa", ...

  8. java进行微信h5支付开发

    最近在做微信支付开发用的框架是 srpingMVC mybatis spring 下面是开发流程图 我们只需要开发红色标记的模块就可以了. 具体参数详情可以查看微信开发者文档. 新手第一次写,写的不好 ...

  9. 12-MySQL-Ubuntu-数据表的查询-数据准备和基本查询(一)

    一,数据准备 创建数据库.数据表 -- 创建数据库 create database python_test_1 charset=utf8; -- 使用数据库 use python_test_1; -- ...

  10. 查看Linux服务器公网IP

    参考:https://www.cnblogs.com/pyyu/p/8545896.html 方法1:curl ifconfig.me 方法2:curl cip.cc