I'm not sure what I'm wanting to do is even a good idea, but here's the problem anyway: I have MyClass which I want to implement two different types of the generic IEnumerable class, e.g.
public class MyClass : IEnumerable<KeyValuePair<string, string>>,
IEnumerable<KeyValuePair<MyEnum, string>>
Now, the problem with doing this is when I try to define necessary methods from the interfaces, the compiler complains "Type 'MyClass' already defines a member called 'GetEnumerator' with the same parameter types". This is because I have these two methods:
public IEnumerator<KeyValuePair<MyEnum, string>> GetEnumerator() { ... }
public IEnumerator<KeyValuePair<string, string>> GetEnumerator() { ... }
I have to have GetEnumerator() with no parameters because of the interface, and the only thing that differs is the return type, which is not allowed.
Here are what I see as my options:
- I was considering having a "main"
IEnumerablegeneric type whichMyClasswould implement, and then just adding extra methods that differ by parameters and not just return type (e.g.Add), without implementing the extra generic interfaces. - I could create a generic base class for
MyClass, call itMyBaseClass<T>, and it would implementIEnumerable<KeyValuePair<T, string>>. Then, I would have different versions ofMyClass, e.g.MyClass<string>andMyClass<MyEnum>.
Which seems preferable here, or am I missing something that would be an even better solution?