1

Having code as below, getting a compile error - "... does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'".

How to implement the non-generic version of GetEnumerator?

public class Giraffe { } public class Pigeon { } public class Sample : IEnumerable<Giraffe>, IEnumerable<Pigeon> { IEnumerator<Giraffe> IEnumerable<Giraffe>.GetEnumerator() { return null; } IEnumerator<Pigeon> IEnumerable<Pigeon>.GetEnumerator() { return null; } } 
2
  • This is probably not going to be particularly helpful, because the compiler will never be able to tell which IEnumerable you'd like your guy to be treated as. Commented Aug 20, 2014 at 2:12
  • Yes, I think so. I read an article, and I'm curious that how to implement it. Commented Aug 20, 2014 at 2:16

2 Answers 2

1

Try:

public class Pets :IEnumerable, IEnumerable<Giraffe>, IEnumerable<Pigeon> { IEnumerator<Giraffe> IEnumerable<Giraffe>.GetEnumerator() { return null; } IEnumerator<Pigeon> IEnumerable<Pigeon>.GetEnumerator() { return null; } public IEnumerator GetEnumerator() { throw new NotImplementedException(); } } 
Sign up to request clarification or add additional context in comments.

3 Comments

IEnumerator actually requires generic type. I had tried your way, but I got another error.
Add using System.Collection to your using list. Its not defined in the System.Collection.Generics namespace.
You should throw a NotSupportedException in this case (since by design the method will not be implemented).
1

if I understand your problem correctly, here is a sample how you can implement non-generic Enumerator in your class

public class Sample : IEnumerable<Giraffe>, IEnumerable<Pigeon> { IEnumerator<Giraffe> IEnumerable<Giraffe>.GetEnumerator() { return null; } IEnumerator<Pigeon> IEnumerable<Pigeon>.GetEnumerator() { return null; } IEnumerator IEnumerable.GetEnumerator() { return null; //your logic for the enumerator } } 

since the generic IEnumerable<T> inherits non-generic IEnumerable so implementing IEnumerable.GetEnumerator would define an implementation for the same.

you may additionally declare the class as public class Sample : IEnumerable, IEnumerable<Giraffe>, IEnumerable<Pigeon> for more clarity

example

public class Sample : IEnumerable, IEnumerable<Giraffe>, IEnumerable<Pigeon> { IEnumerator<Giraffe> IEnumerable<Giraffe>.GetEnumerator() { return null; } IEnumerator<Pigeon> IEnumerable<Pigeon>.GetEnumerator() { return null; } IEnumerator IEnumerable.GetEnumerator() { return null; //your logic for the enumerator } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.