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

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

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

Dictionary implements IEnumerable

相关标签:
7条回答
  • 2021-02-08 13:28

    Add the following explicit interface implementation:

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

    Although IEnumerator<T> is an IEnumerator, the contract for IEnumerable returns an IEnumerator specifically, not an IEnumerator<T>

    0 讨论(0)
  • 2021-02-08 13:37

    When you implement the generic IEnumerable interface, you also have to implement the non generic IEnumerable interface. The error is about the missing non generic method.

    0 讨论(0)
  • 2021-02-08 13:37

    Here's the declaration of IEnumerable:

    public interface IEnumerable<out T> : IEnumerable
    {
        new IEnumerator<T> 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();
        }
    
    0 讨论(0)
  • 2021-02-08 13:44

    When implementing IEnumerable<T>, you must also explicitly implement IEnumerable.GetEnumerator(). The method for the generic interface is not valid in and of itself as an implementation for the non-generic method contract. You can have one call the other, or since you have a child object whose enumerator you are using, just copy/paste;

    using System.Collections;
    using System.Collections.Generic;
    
    public class FooCollection : IEnumerable<Foo>
    {
        private Dictionary<string, Foo> fooDictionary = new Dictionary<string, Foo>();
    
        public IEnumerator<Foo> GetEnumerator()
        {
            return fooDictionary.Values.GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            //forces use of the non-generic implementation on the Values collection
            return ((IEnumerable)fooDictionary.Values).GetEnumerator();
        }
    
        // Other wrapper methods omitted
    
    }
    
    0 讨论(0)
  • 2021-02-08 13:45

    As long as generic IEnumerable{T} inherit IEnumerable You have to implement IEnumerable.GetEnumerator() as well. You can do it explicitly like:

    IEnumerator IEnumerable.GetEnumerator()
    {
           return GetEnumerator();
    }
    
    0 讨论(0)
  • 2021-02-08 13:45

    You've already had several answers to your main question. I'll answer the question raised in your edit...

    The List<T> class actually has three different GetEnumerator methods: The public method that's called when the compile-time instance is typed as List<T> itself, and two explicit interface implementations to meet the IEnumerable/IEnumerable<T> contracts. The enumerator objects returned by all three methods are all the same List<T>.Enumerator type behind-the-scenes.

    // Public method
    public List<T>.Enumerator GetEnumerator() { /* ... */ }
    
    // IEnumerable<T> explicit interface implementation
    IEnumerator<T> IEnumerable<T>.GetEnumerator() { /* ... */ }
    
    // IEnumerable explicit interface implementation
    IEnumerator IEnumerable.GetEnumerator() { /* ... */ }
    
    0 讨论(0)
提交回复
热议问题