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

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

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

Dictionary implements IEnumerable

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

    The problem is that there is no such thing as return type covariance in .NET - IEnumerator M() and IEnumerator<Foo> M() are completely different methods.

    The workaround is that you have to implement the non-generic version explicitly:

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        // this calls the IEnumerator<Foo> GetEnumerator method
        // as explicit method implementations aren't used for method resolution in C#
        // polymorphism (IEnumerator<T> implements IEnumerator)
        // ensures this is type-safe
        return GetEnumerator();
    }
    
    0 讨论(0)
提交回复
热议问题