【发布时间】:2016-05-09 19:49:13
【问题描述】:
在我在训练期间编写的一个类上实现 IEnumerable 和 IEnumerator 时,我注意到我需要为属性“Current”指定两个实现。
public class PeopleEnumerator : IEnumerator<Person>
{
People people; // my collection I'm enumerating (just a wrapped array).
int index; // my index to keep track of where in the collection I am.
...
//implementation for Person
public Person Current { get { return people[index]; } }
//implementation for object
object Enumerator.Current { get { return people[index]; } }
}
我大致了解我正在实现一些非通用版本的 IEnumerator 和它的“当前”属性,但是我不了解以下内容:
- 为什么允许我在这里按返回类型重载?大概它与显式 IEnumerator.Current;但是原因对我来说是不透明的。
- 为什么不能将“object IEnumerator.Current”指定为“public”?
提前致谢!
【问题讨论】:
标签: c#