C# 中的 IEnumerator 和 IEnumerable 接口之间的差异
IEnumerable和IEnumerator在C#中都是接口。
IEnumerable是一个接口,定义一个方法 GetEnumerator(),它返回一个 IEnumerator 接口。
这适用于对实现 IEnumerable 的集合进行只读访问,可以使用 foreach 语句。
IEnumerator有两个方法MoveNext和Reset。它还有个属性叫做Current。
以下显示了IEnumerable和IEnumerator的实现。
示例
class Demo : IEnumerable, IEnumerator {
// IEnumerable method GetEnumerator()
IEnumerator IEnumerable.GetEnumerator() {
throw new NotImplementedException();
}
public object Current {
get { throw new NotImplementedException(); }
}
// IEnumertor method
public bool MoveNext() {
throw new NotImplementedException();
}
// IEnumertor method
public void Reset() {
throw new NotImplementedException();
}
}上面你可以看到IEnumerator的两个方法。
// IEnumertor method
public bool MoveNext() {
throw new NotImplementedException();
}
// IEnumertor method
public void Reset() {
throw new NotImplementedException();
}
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 语言程序设计
C++
C#
MongoDB
MySQL
Javascript
PHP