IEnumerator 枚举使用
来源:互联网 发布:mac安装迅雷 编辑:程序博客网 时间:2024/05/21 14:49
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace TestEffectEntry
{
public class Person
{
public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
}
public string firstName;
public string lastName;
}
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];
}
}
public IEnumerator GetEnumerator()
{
//return new PeopleEnum(_people);
//return _people.GetEnumerator();
for (int i = 0; i < _people.Length; i++)
{
yield return _people[i];
}
}
}
public class PeopleEnum : IEnumerator
{
public Person[] _people;
int position = -1;
public PeopleEnum(Person[] list)
{
_people = list;
}
public bool MoveNext()
{
position++;
return (position < _people.Length);
}
public void Reset()
{
position = -1;
}
public object Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
class Program
{
static void Main(string[] args)
{
Person[] peopleArray = new Person[3];
peopleArray[0] = new Person("John", "Smith");
peopleArray[1] = new Person("Jim", "Johnson");
peopleArray[2] = new Person("Sue", "Rabon");
People peopleList = new People(peopleArray);
// foreach (Person p in peopleList)
// Console.WriteLine(p.firstName + " " + p.lastName);
IEnumerator itor = peopleArray.GetEnumerator();
while (itor.MoveNext())
{
Person p = itor.Current as Person;
Console.WriteLine(p.firstName + " " + p.lastName);
}
Console.ReadKey();
}
}
}
- IEnumerator 枚举使用
- C#枚举数IEnumerator
- IEnumerable和IEnumerator枚举器
- IEnumerator和IEnumerator接口的使用
- C#枚举器接口IEnumerator的实现
- 实现IEnumerator接口的枚举器类型
- c#实现Foreach枚举_IEnumerable和IEnumerator
- IEnumerable和IEnumerator的使用
- 使用IEnumerable和IEnumerator接口
- 协程(IEnumerator)的使用
- 构建可枚举类型(IEnumerable和IEnumerator接口)
- IEnumerable与IEnumerator在C#中的使用
- IEnumerator和IEnumerable是怎样使用的
- IEnumerable与IEnumerator在C#中的使用
- IEnumerable与IEnumerator在C#中的使用
- C#中使用IEnumerator接口遍…
- C#中的IEnumerable和 IEnumerator的使用
- IEnumerable与IEnumerator在C#中的使用
- 关于 MVP 奖励计划
- 如何查看SQL Server 2000数据库每个表占用的磁盘空间
- 随机生成一个根据参数指定的随机数
- VC++6.0利用 IXMLDOMDocumentPtr,IXMLDOMElementPtr生成XML文档
- 返回一个字符串日期
- IEnumerator 枚举使用
- javax.wsdl.Definition 报错!
- javaEmai的使用
- JAVA中获取路径
- 配置 Windows Server 2003-IIS 6
- 第一文
- 一个生成json的java类
- SQL CE 安装
- 配置JDK和Tomcat环境变量