我如何实现 IEnumerable<T> [英] How do I implement IEnumerable<T>

查看:29
本文介绍了我如何实现 IEnumerable<T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何实现非通用 IEnumerable,如下所示:

I know how to implement the non generic IEnumerable, like this:

using System;
using System.Collections;

namespace ConsoleApplication33
{
    class Program
    {
        static void Main(string[] args)
        {
            MyObjects myObjects = new MyObjects();
            myObjects[0] = new MyObject() { Foo = "Hello", Bar = 1 };
            myObjects[1] = new MyObject() { Foo = "World", Bar = 2 };

            foreach (MyObject x in myObjects)
            {
                Console.WriteLine(x.Foo);
                Console.WriteLine(x.Bar);
            }

            Console.ReadLine();
        }
    }

    class MyObject
    {
        public string Foo { get; set; }
        public int Bar { get; set; }
    }

    class MyObjects : IEnumerable
    {
        ArrayList mylist = new ArrayList();

        public MyObject this[int index]
        {
            get { return (MyObject)mylist[index]; }
            set { mylist.Insert(index, value); }
        }

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

不过我也注意到 IEnumerable 有一个通用版本,IEnumerable,但我不知道如何实现它.

However I also notice that IEnumerable has a generic version, IEnumerable<T>, but I can't figure out how to implement it.

如果我将 using System.Collections.Generic; 添加到我的 using 指令中,然后更改:

If I add using System.Collections.Generic; to my using directives, and then change:

class MyObjects : IEnumerable

到:

class MyObjects : IEnumerable<MyObject>

然后右键单击IEnumerable并选择Implement Interface =>实现接口,Visual Studio 帮助添加了以下代码块:

And then right click on IEnumerable<MyObject> and select Implement Interface => Implement Interface, Visual Studio helpfully adds the following block of code:

IEnumerator<MyObject> IEnumerable<MyObject>.GetEnumerator()
{
    throw new NotImplementedException();
}

GetEnumerator(); 方法返回非通用 IEnumerable 对象这次不起作用,那么我在这里放什么?当 CLI 在 foreach 循环期间尝试通过我的数组进行枚举时,它现在会忽略非通用实现并直接转向通用版本.

Returning the non generic IEnumerable object from the GetEnumerator(); method doesn't work this time, so what do I put here? The CLI now ignores the non generic implementation and heads straight for the generic version when it tries to enumerate through my array during the foreach loop.

推荐答案

如果您选择使用泛型集合,例如 List 而不是 ArrayList,您会发现 List 将提供您可以使用的通用和非通用枚举器.

If you choose to use a generic collection, such as List<MyObject> instead of ArrayList, you'll find that the List<MyObject> will provide both generic and non-generic enumerators that you can use.

using System.Collections;

class MyObjects : IEnumerable<MyObject>
{
    List<MyObject> mylist = new List<MyObject>();

    public MyObject this[int index]  
    {  
        get { return mylist[index]; }  
        set { mylist.Insert(index, value); }  
    } 

    public IEnumerator<MyObject> GetEnumerator()
    {
        return mylist.GetEnumerator();
    }

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

这篇关于我如何实现 IEnumerable&lt;T&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆