小编Ale*_*nes的帖子

为什么我必须定义IEnumerator <T> .Current&IEnumerator.Current,它实现了什么?

当我在训练期间编写的类上实现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]; } }
}
Run Code Online (Sandbox Code Playgroud)

我粗略地了解到我正在为它实现一些非泛型版本的IEnumerator和一个"当前"属性,但我不明白以下内容:

  • 为什么我允许在这里通过返回类型重载?据推测它与显式的IEnumerator.Current有关; 但是原因对我来说是不透明的.
  • 为什么"对象IEnumerator.Current"不能被指定为"public"?

提前致谢!

c#

6
推荐指数
1
解决办法
363
查看次数

标签 统计

c# ×1