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. Mysql流程解析

    Mysql流程解析 流程图 流程图解析 客户端发送一条sql语句. 1.此时,mysql会检查sql语句,查看是否命中缓存,如果命中缓存,直接返回结果,不继续执行.没有命中则进入解析器. 2.解析器会 ...

  2. python基础小点

    变量的命名规则 由字母.下划线.数字组成,且不能以数字开头 不能用关键字作为变量名 最好不要与python内置的一些方法和类名冲突 变量名应尽量简短且具有意义,多个单词之间用下划线连接 注释 #  - ...

  3. Number 的扩展

    Number.parseInt(), Number.parseFloat() ES6 将全局方法parseInt()和parseFloat(),移植到Number对象上面,行为完全保持不变. Numb ...

  4. 2、go 运行操作

    1)有且只有一个函数入口,main liteIDE,直接图形界面编译,一个文件夹里的只能有一个main函数 或者 go build XXX.go 编译go代码,生成一个可执行程序 然后运行可执行程序 ...

  5. thinkphp入口文件

    ThinkPHP采用单一入口模式进行项目部署和访问,无论完成什么功能,一个应用都有一个统一(但不一定是唯一)的入口. 应该说,所有应用都是从入口文件开始的,并且不同应用的入口文件是类似的. 入口文件定 ...

  6. 数位dp——牛客多校H

    /* x[1,A] y[1,B] x^y<C 或 x&y>C 把ABC拆成二进制后按位进行数位dp dp[pos][s1][s2][f1][f2] 表示从高到低第pos位,条件一状 ...

  7. vue-组件之间的通信:

    组件之间的通信:一个组件被调用,那么里面的数据就需要从前者调用,因为在开发中组件时重复调用的,在页面中会反复使用,但是里面的数据是不一样的,谁调用这个组件谁就传递数据给这个组件,所以就要暴露一些接口, ...

  8. Spring-Security (补充)

    一.配置静态资源过滤 直接在xml中配置即可 <!-- 配置静态资源过滤 --> <security:http security="none" pattern=& ...

  9. Greenplum(PostgreSql)使用 with recursive 实现树形结构递归查询并插入新表

    本代码目的是替代Oracle的connect by语句,并实现后者的path和idleaf功能. 正文开始: 假设表org,字段有 id(编号),name(名称),pid(上级编号), 最上级的记录p ...

  10. 数据结构C++版-图

    一.概念及分类 二.图的存储结构 1.邻接矩阵 顶点: 弧: 边: 表达式语句: 2.邻接表 逆邻接表: 3.十字链表 4.邻接多重表 三.图的权值概念及遍历 权值: 图的遍历: 1.深度优先搜索 2 ...