与IEnumerable <t>实现相关的显式接口实现?</t>

时间:2014-10-30 03:04:16

标签: c# .net interface ienumerable explicit-interface

为什么在this MSDN示例中需要 GetEnumerator1 方法?

// Must implement GetEnumerator, which returns a new StreamReaderEnumerator. 
public IEnumerator<string> GetEnumerator()
{
    return new StreamReaderEnumerator(_filePath);
}

// Must also implement IEnumerable.GetEnumerator, but implement as a private method. 
private IEnumerator GetEnumerator1()
{
    return this.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
    return GetEnumerator1();
}

1 个答案:

答案 0 :(得分:1)

不需要,我们可以使用

// Must implement GetEnumerator, which returns a new StreamReaderEnumerator. 

public IEnumerator<string> GetEnumerator()
{
    return new StreamReaderEnumerator(_filePath);
}

// Must also implement IEnumerable.GetEnumerator, but implement as a private method. 

IEnumerator IEnumerable.GetEnumerator()
{
    return GetEnumerator();
}

代替。