Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

ienumerable

I'm trying to create a wrapper for a Dictionary<String,Foo>.

Dictionary<String,Foo> implements IEnumerable<KeyValuePair<String,Foo>>, but I want my wrapper class to implement IEnumerable<Foo>. So I tried this:

public class FooCollection : IEnumerable<Foo>
{
    private Dictionary<string, Foo> fooDictionary = new Dictionary<string, Foo>();

    public IEnumerator<Foo> GetEnumerator()
    {
        return fooDictionary.Values.GetEnumerator();
    }

    // Other wrapper methods omitted

}

However I get this error:

'FooCollection' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'FooCollection.GetEnumerator()' cannot implement 'System.Collections.IEnumerable.GetEnumerator()' because it does not have the matching return type of 'System.Collections.IEnumerator'.

However I don't understand this error, because FooCollection.GetEnumerator() returns an IEnumerator<Foo>, and IEnumerator<Foo> is an IEnumerator.

EDIT:

The solution of explicitly implementing IEnumerator.GetEnumerator() works. However I'm now wondering why when I "Go to definition" on a List<T> I see only one definition of GetEnumerator: public List<T>.Enumerator GetEnumerator();

Apparently List<T> can have a single GetEnumerator method that returns something that implements both IEnumerator<T> and IEnumerator, but I have to have one method for each?

EDIT:

As answered by LukeH below, List<T> does include the explicit interface implementations. Apparently Visual Studio just doesn't list those when generating method stubs from the metadata. (See this previous question: Why does the VS Metadata view does not display explicit interface implemented members )

Before posting this question I had tried checking List<T> (via "Go to Definition" in Visual Studio) to see if I needed to implement multiple versions of GetEnumerator. I guess this wasn't the most reliable way to check.

Anyway, I'm marking this as answered. Thanks for your help.

like image 653
Tim Goodman Avatar asked Oct 07 '11 20:10

Tim Goodman


People also ask

How is IEnumerable implemented?

IEnumerable is supposed to be implemented by the collection, the AbstractList in the diagram above. This interface defines the GetEnumerator() method, which is called CreateIterator() in the diagram. IEnumerator interface defines the abstract iterator.

What is IEnumerable in C# with example?

IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface. It is the base interface for all non-generic collections that can be enumerated. This works for read-only access to a collection that implements that IEnumerable can be used with a foreach statement.

What inherits IEnumerable?

ICollection inherits from IEnumerable. You therefore have all members from the IEnumerable interface implemented in all classes that implement the ICollection interface.

What are Enumerables in C#?

In C#, an Enumerable is an object like an array, list, or any other sort of collection that implements the IEnumerable interface. Enumerables standardize looping over collections, and enables the use of LINQ query syntax, as well as other useful extension methods like List.


1 Answers

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>

like image 124
Matthew Abbott Avatar answered Oct 21 '22 18:10

Matthew Abbott