public interface IEnumerator<out T> : IEnumerator 泛型接口继承原本的接口?

在网上看迭代器时看到的,觉得好奇怪,C#中接口能继承原本的接口?
链接:https://referencesource.micro...

  public interface IEnumerator<out T> : IDisposable, IEnumerator
    {    
        // Returns the current element of the enumeration. The returned value is
        // undefined before the first call to MoveNext and following a
        // call to MoveNext that returned false. Multiple calls to
        // GetCurrent with no intervening calls to MoveNext 
        // will return the same object.
        // 
        /// <include file='doc\IEnumerator.uex' path='docs/doc[@for="IEnumerator.Current"]/*' />
        new T Current {
            get; 
        }
    }
阅读 2.7k
1 个回答
新手上路,请多包涵

IEnumerator<out T>IEnumerator是两个完全不同的接口,尽管看起来拼写一样,但签名并不一样。

  • IEnumerator包含三个成员:bool MoveNext();Object Current { get; }void Reset();
  • IEnumerator<out T>包含一个成员:new T Current { get; }

这就意味着在实现IEnumerator<out T>时你要实现MoveNext()以及Reset()两个方法以及两个版本的Current{ get; }:一个返回T、另一个返回Object。当然在实现这两个版本时你可以复用代码。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进