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.