如何在 C# 中将类设为 IEnumerable?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13135443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:42:24  来源:igfitidea点击:

How to make the class as an IEnumerable in C#?

c#classienumerable

提问by Ivan Prodanov

So I've got a class and a generic List inside of it, but it is private.

所以我有一个类和一个通用列表,但它是私有的。

class Contacts
{
    List<Contact> contacts;
    ...
}

I want to make the class work as this would do:

我想让这门课像这样工作:

foreach(Contact in contacts) .... ;

like this (not working):

像这样(不工作):

Contacts c;
foreach(Contact in c) .... ;

In the example above the Contact class instance c has to yield return every item from contacts(private member of c)

在上面的例子中,Contact 类实例 c 必须从contacts(c 的私有成员)返回每个项目

How do I do it? I know it has to be IEnumerable with yield return, but where to declare that?

我该怎么做?我知道它必须是具有收益回报的 IEnumerable,但是在哪里声明呢?

回答by LightStriker

Implement the interface IEnumerable:

实现接口IEnumerable:

class Contacts : IEnumerable<Contact>
{
    List<Contact> contacts;

    #region Implementation of IEnumerable
    public IEnumerator<Contact> GetEnumerator()
    {
        return contacts.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
    #endregion
}

回答by Tigran

public class Contacts: IEnumerable
{
     ...... 
    public IEnumerator GetEnumerator()
    {
        return contacts.GetEnumerator();
    }
}

Should do a trick for you.

应该为你做一个伎俩。

回答by Tim Schmelter

Or return an IEnumerator<Contact>by providing a GetEnumeratormethod:

或者IEnumerator<Contact>通过提供一个GetEnumerator方法返回一个:

class Contacts
{
    List<Contact> contacts;

    public IEnumerator<Contact> GetEnumerator()
    {
        foreach (var contact in contacts)
            yield return contact;
    }
}

The foreachlooks for GetEnumerator. Have a look here for the language specification details regarding this: https://stackoverflow.com/a/3679993/284240

foreach验看GetEnumerator。在此处查看有关此的语言规范详细信息:https: //stackoverflow.com/a/3679993/284240

How to make a Visual C# class usable in a foreach statement

如何使 Visual C# 类在 foreach 语句中可用

回答by flaugh

class Program
{
    static void Main(string[] args)
    {
        var list = new Contacts();
        var a = new Contact() { Name = "a" };
        var b = new Contact() { Name = "b" };
        var c = new Contact() { Name = "c" };
        var d = new Contact() { Name = "d" };
        list.ContactList = new List<Contact>();
        list.ContactList.Add(a);
        list.ContactList.Add(b);
        list.ContactList.Add(c);
        list.ContactList.Add(d);

        foreach (var i in list)
        {
            Console.WriteLine(i.Name);
        }
    }
}

class Contacts : IEnumerable<Contact>
{
    public List<Contact> ContactList { get; set; }

    public IEnumerator<Contact> GetEnumerator()
    {
        return ContactList.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return ContactList.GetEnumerator();
    }
}

class Contact
{
    public string Name { get; set; }
}

回答by Stephanie

How about just extending List<Contact>

只是延长怎么样 List<Contact>

If you don't want to extend any other class its a very simple, fast option:

如果您不想扩展任何其他类,这是一个非常简单、快速的选择:

class Contacts :List<Contact>
{   
}