IEnumerable是所有可迭代非范型类的基础接口。IEnumerable包括一个方法GetEnumerator方法,方法返回一个IEnumerator。
IEnumerator是所有非范型迭代器的基础接口。foreach语句隐藏了C#迭代器的复杂实现。推荐使用foreach代替直接操作迭代器。 迭代器可以读取集合中的数据,但是不能从底层修改集合。 初始的时候,迭代器定位在集合的第一个元素前面,在读取Current值之前需要调用一次MoveNext将迭代器驱动到第一个元素的位置。 Current一直返回相同的元素直到调用了MoveNext或者Reset方法。MoveNext将Current推进到下一个元素。 如果MoveNext之后position超出了集合的范围,MoveNext将返回false。 通过调用Reset将Current重置到第一个元素之前。
于是我们不难得出
class Program
{
static void Main(string[] args)
{
Person[] peopleArray = new Person[3]
{
new Person("John","Smith"),
new Person("Tom","Johnson"),
new Person("Sue","Robon"),
};
People peopleList = new People(peopleArray);
foreach (Person person in peopleList)
{
Console.WriteLine(person.firstName + person.lastName);
}
IEnumerator enumerator = peopleList.GetEnumerator();
while (enumerator.MoveNext())
{
Person person = (Person)enumerator.Current;
Console.WriteLine(person.firstName + person.lastName);
}
}
}
上面两种写法是一样的,foreach是一种语法糖,简化了遍历其中的具体实现。被遍历的类通过实现IEnumerable接口和实现一个IEnumerator枚举器实现遍历功能。
使用实例:
class Program
{
static void Main(string[] args)
{
Person[] peopleArray = new Person[3]
{
new Person("John","Smith"),
new Person("Tom","Johnson"),
new Person("Sue","Robon"),
};
People peopleList = new People(peopleArray);
foreach (Person person in peopleList)
{
Console.WriteLine(person.firstName + person.lastName);
}
}
}
public class Person
{
public string firstName;
public string lastName;
public Person(string firstName, string lastName)
{
this.firstName = firstName;
this.lastName = 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];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)GetEnumerator();
}
public PeopleEnum GetEnumerator()
{
return new PeopleEnum(_people);
}
}
public class PeopleEnum:IEnumerator
{
public Person[] _people;
int position = -1;
public PeopleEnum(Person[] list)
{
_people = list;
}
Object IEnumerator.Current
{
get
{
return Current;
}
}
public Person Current
{
get
{
try
{
return _people[position];
}
catch(IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
public bool MoveNext()
{
position++;
return (position < _people.Length);
}
public void Reset()
{
position = -1;
}
}