我如何实现IEnumerable< T>如何实现、IEnumerable、GT、LT

2023-09-02 11:47:15 作者:油皮

我知道如何实现非泛型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< T> ,但我不能想出如何实现它

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

如果我加入使用System.Collections.Generic; 来我使用的指令,然后更改:

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

class MyObjects : IEnumerable

class MyObjects : IEnumerable<MyObject>

,然后右键单击的IEnumerable&LT;为MyObject&GT; ,然后选择实现接口=&GT;实现接口,Visual Studio的帮忙,增加了code以下块:

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.

感谢

推荐答案

如果您选择使用泛型集合,如名单,其中,为MyObject&GT; 而不是的ArrayList ,你会发现名单,其中,为MyObject&GT; 将提供通用和非通用枚举,您可以使用

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.

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();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}
 
精彩推荐
图片推荐