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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:54:28+00:00 2026-06-09T06:54:28+00:00

I have been wondering if it is possible to make say State class implementing

  • 0

I have been wondering if it is possible to make say State class implementing IEnumerable<Person> and IEnumerable<City> so I could get all the people living in the state via foreach as well as all the cities. It won’t even compile saying this: Error 1 'ConsoleApplication1.City' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'(weird)… Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections;

namespace ConsoleApplication1
{
    class Person
    {
    }

    class City : IEnumerable<Person>
    {
        // City has citizens:
        Person[] citizens;

        IEnumerator<Person> IEnumerable<Person>.GetEnumerator()
        {
            foreach (Person p in citizens)
                yield return p;
        }
    }

    class State : IEnumerable<Person>, IEnumerable<City>
    {
        // State has cities:
        City[] cities;

        IEnumerator<Person> IEnumerable<Person>.GetEnumerator()
        {
            foreach (City c in cities)
                foreach (Person p in c)
                    yield return p;
        }

        IEnumerator<City> IEnumerable<City>.GetEnumerator()
        {
            foreach (City c in cities)
                yield return c;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            State s = new State();
            foreach (Person p in s) ;
            foreach (City c in s) ;
        }
    }
}
  • 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-06-09T06:54:31+00:00Added an answer on June 9, 2026 at 6:54 am

    The problem is that IEnumerable<T> also requires you to implement IEnumerable (the non generic version). You need to implement both GetEnumerator() calls.

    That being said, this will get very tricky, as you’re State class will need to determine which thing to enumerate. I would, personally, avoid implementing IEnumerable<T> twice in one class, and instead return the enumerable as a method:

    class State : IEnumerable<City>
    {
        public IEnumerable<Person> GetPeople()
        {
          // return people...
    

    In general, I think that trying to make something be an enumeration of two separate types is really a design flaw. It would be better to have State be implemented more like:

    public class State
    {
        public IEnumerable<City> Cities { get { // return cities...
    
        public IEnumerable<People> People { get { // return people...
    

    This would require you to change your usage (slightly), to be more like:

    foreach(Person person in theState.People)
    {
        // ....
    

    Personally, I think this would be a better approach for both State and City. I would write this like:

    using System.Collections.Generic;
    using System.Linq;
    
    namespace ConsoleApplication1
    {
    
        class Person
        {
        }
    
        class City
        {
            // City has citizens:
            Person[] citizens;
    
            public IEnumerable<Person> People
            {
                get
                {
                    return citizens;
                }
            }
        }
    
        class State : IEnumerable<Person>, IEnumerable<City>
        {
            // State has cities:
            City[] cities;
    
            public IEnumerable<City> Cities
            {
                get
                {
                    return cities;
                }
            }
    
            public IEnumerable<Person> AllPeople
            {
                get
                {
                    return Cities.SelectMany(c => c.People);
                }
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                State s = new State();
                foreach (Person p in s.AllPeople) { /* Do something */ }
                foreach (City c in s.Cities) { /* Do something */ } 
            }
        }
    }
    

    I find this is much more clear – as a City has People, but it is not, itself, People, etc.

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

Sidebar

Related Questions

I have been wondering about how people manage their strings if they are likely
I have been wondering about the following lines of code [self performSelector:@selector(myMethod) withObject:self afterDelay:1.0];
I have been wondering and couldn't find a clear answer on this subject. Imagine
I have been wondering about the reload() function in python, which seems like it
I have been wondering about the performance of regular expression implementations lately, and have
Something I have been wondering about properties for a while. When you are using
Lately i have been wondering if there is a performance difference between repeating the
I've been hacking around with Ruby a little and I have been wondering if
Hello android designers, 1-I have been wondering. Is it a good design approach to
I ran into this RegExp /[[0]]/ in JavaScript, and have been wondering what it

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.