Lars1346

Code Snippet

public IEnumerator GetEnumerator()

{

List<IResult> list = new List<IResult>();

// code

return list.GetEnumerator();

}

The above code worked fine when implementing IEnumerable.

Decided that I'd want to switch to IEnumerable<T>. I then implemented the following code;

Code Snippet

public IEnumerator<IResult> GetEnumerator()

{

List<IResult> list = new List<IResult>();

return list.GetEnumerator();

}

But end up getting this compile error in vs2005

Error 1 'foo' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'foo.GetEnumerator()' is either static, not public, or has the wrong return type.

What am I doing wrong



Re: Visual Studio Code Analysis and Code Metrics IEnumerable<T>

bruno_1

yes this is right, cause there are 2 different methods to implement (because there are 2 interfaces), so you should explicitly implement the GetEnumerator() for the IEnumerator interface while implementaing IEnumerator implicitly (or the inverse, it depends on which interface you prefer to use in code without casting), so this mean, you should do so;ething similar to:
public IEnumerator<IResult> GetEnumerator()
{
// some implementation
}
public IEnumerator System.Collection.IEnumerable.GetEnumerator()
{
// Maybe other implementation, or you can just call the above by: return GetEnumerator()
}

Hope this help.





Re: Visual Studio Code Analysis and Code Metrics IEnumerable<T>

Lars1346

Cheers mate.

Ended up with this code (had to remove 'public' from your example and add an 's') ;

Code Snippet

IEnumerator System.Collections.IEnumerable.GetEnumerator()

{

return this.GetEnumerator();

}