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. GIT 部分记录

    关于版本回退 git reset HEAD^  #回退a.py这个文件的版本到上一个版本  git reset HEAD^ a.py  git reset HEAD a.py  我试了一下以上2种方式 ...

  2. Java微服务(Spring-boot+MyBatis+Maven)入门教程

    1,项目创建    新建maven项目,如下图: 选择路径,下一步 输入1和2的内容,点完成 项目创建完毕,结构如下图所示: 填写pom.xml里内容,为了用于打包,3必须选择jar,4和5按图上填写 ...

  3. PagedLOD模型对象选择关键技术点

    DatabaseCacheReadCallback这个类继承ReadCallback,在相交的测试中,场景可能有PagedLOD,而计算相交过程中,PagedLOD不是精度最高的节点,这样计算的就不准 ...

  4. Q:简单实现URL只能页面跳转,禁止直接访问

    sessionStorage 用于临时保存同一窗口(或标签页)的数据,在关闭窗口或标签页之后将会删除这些数据,且不同标签页的session不能共享,通过此特性来控制某个页面只能通过上级页面同标签页跳转 ...

  5. 【JZOJ3293】【BZOJ4416】【luoguP3989】阶乘字符串

    description 给定一个由前n个小写字母组成的串S. 串S是阶乘字符串当且仅当前n个小写字母的全排列(共n!种)都作为S的子序列(可以不连续)出现. 由这个定义出发,可以得到一个简单的枚举法去 ...

  6. thinkphp 日志驱动

    日志驱动默认的命名空间位于Think\Log\Driver,驱动类需要实现的接口方法包括: 方法 说明 架构方法 __construct($config=array()) 写入方法 write($lo ...

  7. NX二次开发-UFUN修剪体UF_MODL_trim_body

    1 NX11+VS2013 2 3 4 #include <uf.h> 5 #include <uf_modl.h> 6 7 8 UF_initialize(); 9 10 / ...

  8. [JZOJ 5813] 计算

    题意:求满足题意的方案数. 思路: 显然的计数类\(dp\). 不难发现,令$f(x) = \prod_{i=1}^{2m}{x_i} \(. 在找一个\)x'\(使得\)f(x') = \prod_ ...

  9. python入门 类的继承和聚合(五)

    继承 class Rocket: def __init__(self, name, distance): self.name = name self.distance = distance def l ...

  10. 创建用户, 使用crontab定时运行程序

    # 以创建一个名为openstack的用户为例子 sudo adduser openstack sudo adduser openstack sudo # 把openstack用户加到可以使用cron ...