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.
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.
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.
ICollection inherits from IEnumerable. You therefore have all members from the IEnumerable interface implemented in all classes that implement the ICollection interface.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With