public interface IEnumerable
{
IEnumerator GetEnumerator();
}
public interface IEnumerator
{
bool MoveNext();
void Reset();
Object Current { get; }
}
1. 一个集合要支持Foreach语句,必须实现接口IEnumerable接口,即必须实现IEnumerable接口的GetEnumerator方法;通常实现GetEnumerator方法时需要用yield return 返回值;
2. IEnumerator 是所有枚举数的基接口。枚举数只允许读取集合中的数据。枚举数无法用于修改基础集合。 这也是为什么说“不能在foreach循环中修改元素的原因 “.