Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 320187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:43:49+00:00 2026-05-12T08:43:49+00:00

I’m not sure what I’m wanting to do is even a good idea, but

  • 0

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:

  1. I was considering having a “main” IEnumerable generic type which MyClass would 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.
  2. I could create a generic base class for MyClass, call it MyBaseClass<T>, and it would implement IEnumerable<KeyValuePair<T, string>>. Then, I would have different versions of MyClass, e.g. MyClass<string> and MyClass<MyEnum>.

Which seems preferable here, or am I missing something that would be an even better solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:43:50+00:00Added an answer on May 12, 2026 at 8:43 am

    You can use explicit interface declarations in order to get different implementations for each of the two interfaces that you are implement. For example:

    public class MyClass : IEnumerable<KeyValuePair<string, string>>,
                       IEnumerable<KeyValuePair<MyEnum, string>>
    {
        IEnumerator<KeyValuePair<MyEnum, string>> IEnumerable<KeyValuePair<MyEnum, string>>.GetEnumerator()
        {
            // return your enumerator here
        }
    
        IEnumerator<KeyValuePair<string, string>> IEnumerable<KeyValuePair<string, string>>.GetEnumerator()
        {
            // return your enumerator here
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            var me = this as IEnumerable<KeyValuePair<string, string>>;
            return me.GetEnumerator();
        }
    }
    

    However, because IEnumerable<> derives from IEnumerable, you’ll have to pick which one you want to return from the IEnumerable.GetEnumerator() call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I want to construct a data frame in an Rcpp function, but when I
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.