在网上看迭代器时看到的,觉得好奇怪,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;
}
}
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
。当然在实现这两个版本时你可以复用代码。