简体   繁体   English

有关C#中IEnumerator.GetEnumerator的问题

[英]A question about IEnumerator.GetEnumerator in C#

I have a question about the IEnumerator.GetEnumerator() method. 我对IEnumerator.GetEnumerator()方法有疑问。

public class NodeFull
{
    public enum Base : byte {A = 0, C, G, U };
    private int taxID;
    private List<int> children;

    public int TaxID
    {
        get { return taxID; }
        set { taxID = value; }
    }

    public int this[int i]
    {
        get { return children[i]; }
        set { children[i] = value; }
    }

    public IEnumerator GetEnumerator()
    {
        return (children as IEnumerator).GetEnumerator();
    }

    public TaxNodeFull(int taxID)
    {
        this.taxID = taxID;
        this.children = new List<int>(3);
    }
}

When I try to compile, error message says 当我尝试编译时,错误消息显示

'System.Collections.IEnumerator' does not contain a definition for 'GetEnumerator' and no extension method 'GetEnumerator' accepting a first argument of type 'System.Collections.IEnumerator' could be found (are you missing a using directive or an assembly reference?) 'System.Collections.IEnumerator'不包含'GetEnumerator'的定义,并且找不到扩展方法'GetEnumerator'接受类型为'System.Collections.IEnumerator'的第一个参数(是否缺少using指令或程序集引用?)

Is there anything wrong with the code? 代码有什么问题吗?

Thanks in advance 提前致谢


Thank you guys. 感谢大伙们。 I got it. 我知道了。

It's IEnumerable.GetEnumerator() (or IEnumerable<T>.GetEnumerator() ), not IEnumerator.GetEnumerator() . 它是IEnumerable.GetEnumerator() (或IEnumerable<T>.GetEnumerator() ),而不是IEnumerator.GetEnumerator() The members on IEnumerator are MoveNext() , Current and Reset() (and Dispose for the generic version). IEnumerator上的成员是MoveNext()CurrentReset() (对于通用版本,则为Dispose )。 The IEnumerable is "something that can be iterated over" (eg a list) and the IEnumerator represents the current state within that iteration - like a database cursor. IEnumerable是“可以迭代的东西”(例如,列表),而IEnumerator表示该迭代中的当前状态-类似于数据库游标。

It's slightly odd that your class doesn't implement IEnumerable or IEnumerable<T> itself. 您的类本身未实现IEnumerableIEnumerable<T>有点奇怪。 I'd expect something like this: 我希望这样的事情:

class NodeFull : IEnumerable<int>
{
    ... other stuff as normal ...

    public IEnumerator<int> GetEnumerator()
    {
        return children.GetEnumerator();
    }

    // Use explicit interface implementation as there's a naming
    // clash. This is a standard pattern for implementing IEnumerable<T>.
    IEnumerator IEnumerable.GetEnumerator()
    {
        // Defer to generic version
        return GetEnumerator();
    }
}

children is List<int> , which implements IEnumerable<int> and IEnumerable . 子级是List<int> ,它实现IEnumerable<int>IEnumerable The GetEnumerator() method is defined for those interfaces, not for IEnumerator . GetEnumerator()方法是为那些接口而不是IEnumerator定义的。

children as IEnumerator should result in null. children as IEnumerator应为null。

There is no GetEnumerator() method on the IEnumerator interface. IEnumerator接口上没有GetEnumerator()方法。 Are you trying to use the IEnumerABLE interface perhaps? 您是否正在尝试使用IEnumerABLE接口?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM