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 6711455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:08:31+00:00 2026-05-26T08:08:31+00:00

I think I understand why IEnumerable<T> inherit from IEnumerable , after reading the post:

  • 0

I think I understand why IEnumerable<T> inherit from IEnumerable, after reading the post:
Why does IEnumerable<T> inherit from IEnumerable?

However, I am not sure how best to implement the non-generic method when appling 2 generic interfaces? Here is an example of the code I am writting:

public interface IComponentA { /* ... Interface A Code ... */ }

public interface IComponentB { /* ... Interface B Code ... */ }

public class ComponentModel: IEnumerable<IComponentA>, IEnumerable<IComponentB>
{
    public ComponentModel() { }

    private List<IComponentA> ListOfComponentA = new List<IComponentA>();
    private List<IComponentB> ListOfComponentB = new List<IComponentB>();

    // ... Some public methods to add and remove components (for A and B).

    IEnumerator<IComponentA> IEnumerable<IComponentA>.GetEnumerator()
    {
        return ListOfComponentA.GetEnumerator();
    }

    IEnumerator<IComponentB> IEnumerable<IComponentB>.GetEnumerator()
    {
        return ListOfComponentB.GetEnumerator();
    }

    // The fact that IEnumerable<T> inherits from the non-generic IEnumerable
    // now means I have to deal with this.
    IEnumerator IEnumerable.GetEnumerator()
    {
        // Throwing a NotImplementedException is probably not a good idea
        // so what should I put in here?
        throw new NotImplementedException();
    }
}

Suggestions of what to put in the non-generic method are welcome please.

  • 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-26T08:08:31+00:00Added an answer on May 26, 2026 at 8:08 am

    I probably wouldn’t do that, myself. It can be confusing for a user to have the enumerator enumerate over different things depending on the interface reference calling it, and of course the issue of what the generic returns as well.

    Instead, you could just expose a read-only-ish version of the lists as an iterator:

    public class ComponentModel
    {
        public ComponentModel() { }
    
        private List<IComponentA> ListOfComponentA = new List<IComponentA>();
        private List<IComponentB> ListOfComponentB = new List<IComponentB>();
    
        public IEnumerable<IComponentA> AComponents 
        {
            get { return ListOfComponentA.Skip(0); }
        }
    
        public IEnumerable<IComponentB> BComponents
        {
            get { return ListOfComponentB.Skip(0); }
        }
    
        ...
    }
    

    By using the Skip(0) you return an iterator, and it prevents them from casting back to List<IComponentA> and modifying the List out from under you.

    You could also use a ReadOnlyCollection of course, but those are kinda clunky since they throw when you try to do mutating ops.

    So now, you can iterate over either:

    foreach(var a in myModel.AComponents)
    {
        ...
    }
    
    foreach(var b in myModel.BComponents)
    {
        ...
    }
    

    Also, IF A and B component lists always have the same length, you could have an enumerator over a Tuple of them in .NET 4.0 and using the Linq Zip() method:

    public IEnumerable<Tuple<IComponetA, IComponetB>> Components
    {
        get
        {
            return ListOfComponentA.Zip(ListOfComponentB, (a,b) => Tuple.Create(a,b));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I come from a background of MoM. I think I understand ESB conceptually. However,
I think I understand the use of IntPtr, though I'm really not sure. I
I've been reading a lot about closures and I think I understand them, but
I understand (I think) that XmlHttpRequest objects adhere to the same-domain policy. However, I
I'm new to Scala, and from what I understand yield in Scala is not
I think I understand retain/release in objective-C for the most part. However, I have
I think I understand how loaders are supposed to work, etc, but I'm not
I think I understand the error message: CoreData could not fulfill a fault, but
I think I understand unit testing. But I was wondering: is there a way
I think I understand the basic principals of T4 but I'm having a hard

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.