如果某个对象需要支持foreach
操作,
则应该实现IEnumerable
, IEnumerator
两个接口。
需要实现以下方法:
public object Current{
get{
return ...
}
}
public bool MoveNext(){
...
}
public void Reset(){
...
}
public IEnumerator GetEnumerator()
{
return this;
}
一般要内部维护一个字段,例如:
private int position = -1;