C#实现泛型IEnumerable

C#实现泛型IEnumerable,c#,generics,interface,ienumerable,C#,Generics,Interface,Ienumerable,我一直在想,是否有可能让sayState类实现IEnumerable和IEnumerable,这样我就可以通过foreach获得居住在该州以及所有城市的所有人。它甚至不会编译说:错误1“ConsoleApplication1.City”没有实现接口成员“System.Collections.IEnumerable.GetEnumerator()”(奇怪)。。。代码如下: using System; using System.Collections.Generic; using System.Li

我一直在想,是否有可能让say
State
类实现
IEnumerable
IEnumerable
,这样我就可以通过foreach获得居住在该州以及所有城市的所有人。它甚至不会编译说:
错误1“ConsoleApplication1.City”没有实现接口成员“System.Collections.IEnumerable.GetEnumerator()”
(奇怪)。。。代码如下:

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) ;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统集合;
命名空间控制台应用程序1
{
班主任
{
}
类别城市:IEnumerable
{
//城市有市民:
个人[]公民;
IEnumerator IEnumerable.GetEnumerator()
{
foreach(公民中的p人)
收益率p;
}
}
类状态:IEnumerable,IEnumerable
{
//该州有以下城市:
城市[]个城市;
IEnumerator IEnumerable.GetEnumerator()
{
foreach(城市中的城市c)
foreach(c中的p人)
收益率p;
}
IEnumerator IEnumerable.GetEnumerator()
{
foreach(城市中的城市c)
收益率c;
}
}
班级计划
{
静态void Main(字符串[]参数)
{
状态s=新状态();
foreach(s中的人p);
foreach(南部城市c);
}
}
}

您还需要实现非通用变体! System.Collections.IEnumerable是不带泛型类型参数的参数


为System.Collections.Generic.IEnumerable.GetEnumerator添加一个显式接口实现-并使其引发异常-您无法正确实现非泛型接口。

问题是
IEnumerable
还要求您实现
IEnumerable
(非泛型版本)。您需要实现这两个
GetEnumerator()
调用

这就是说,这将变得非常棘手,因为您的
State
类将需要确定要枚举的内容。我个人会避免在一个类中实现两次
IEnumerable
,而是将枚举作为方法返回:

class State : IEnumerable<City>
{
    public IEnumerable<Person> GetPeople()
    {
      // return people...
就我个人而言,我认为这对
州和
市都是一个更好的方法。我会这样写:

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 */ } 
        }
    }
}
使用System.Collections.Generic;
使用System.Linq;
命名空间控制台应用程序1
{
班主任
{
}
阶级城市
{
//城市有市民:
个人[]公民;
公众人数
{
得到
{
返回公民;
}
}
}
类状态:IEnumerable,IEnumerable
{
//该州有以下城市:
城市[]个城市;
公共数字城市
{
得到
{
回归城市;
}
}
公众可数所有人
{
得到
{
返回城市。选择many(c=>c.People);
}
}
}
班级计划
{
静态void Main(字符串[]参数)
{
状态s=新状态();
foreach(s.AllPeople中的人p){/*做某事*/}
foreach(s.Cities中的城市c){/*做点什么*/}
}
}
}

我发现这要清楚得多——因为一个城市有人,但它本身不是人,等等。

这实际上是一个有趣的问题,因为里德和塞巴斯蒂安都指出,你也需要实现非通用方法。但是,您只能实现一次,并且您有两个需要的接口。我认为更好的设计可能根本不是让州实现IEnumerable,而是有两个属性—人和城市—公开IEnumerable。我认为这也是一个更方便(可发现)的API。

既然我想返回所有的人或所有的citiesDarnit,那么我应该如何实现它,在我发布我的答案时,你用同样的建议更新了你的答案:谢谢大家,我现在知道我想知道的了。
foreach(Person person in theState.People)
{
    // ....
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 */ } 
        }
    }
}