How do I implement IEnumerable in my Dictionary wrapper class that implements IEnumerable?

后端 未结 7 2030
走了就别回头了
走了就别回头了 2021-02-08 12:56

I\'m trying to create a wrapper for a Dictionary.

Dictionary implements IEnumerable

7条回答
  •  孤城傲影
    2021-02-08 13:37

    Here's the declaration of IEnumerable:

    public interface IEnumerable : IEnumerable
    {
        new IEnumerator GetEnumerator();
    }
    

    notice the new keyword.

    Here's the declaration of IEnumerable:

    public interface IEnumerable
    {
    
        IEnumerator GetEnumerator();
    }
    

    So now you have a GetEnumerator method, but which one of those two are you implementing? Therefore, you need to add an explicit implementation of the non-generic version:

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    

提交回复
热议问题