using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Demo4
{
class Person : IEnumerable,IEnumerator
{
int index = -1;
private string[] name = new string[5];
public Person()
{
for (int i = 0; i < name.Length; i++)
{
name[i] = "Name_" + i.ToString();
}
}
#region IEnumerable 成员
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
#endregion
#region IEnumerator 成员
public object Current
{
get
{
return name[index];
}
}
public bool MoveNext()
{
++index;
return index < name.Length ? true : false;
}
public void Reset()
{
index = -1;
}
#endregion
static void Main(string[] args)
{
Person per = new Person();
foreach (string s in per)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}//class
}//namespace
/*
IEnumerator:
GetEnumerator() 返回可循环访问集合的枚举数。
IEnumerator:
object Current 获取集合中的当前元素。
bool MoveNext() 将枚举数推进到集合的下一个元素。
如果枚举数成功地推进到下一个元素,则为 true;如果枚举数越过集合的结尾,则为 false。
void Reset() 将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。
*/