为了账号安全,请及时绑定邮箱和手机立即绑定

如何实现IEnumerable <T>

如何实现IEnumerable <T>

慕尼黑的夜晚无繁华 2019-11-26 15:30:45
我知道如何实现非通用IEnumerable,如下所示: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>,但我不知道如何实现它。如果添加using System.Collections.Generic;到我的using指令,然后更改:class MyObjects : IEnumerable至:class MyObjects : IEnumerable<MyObject>然后右键单击IEnumerable<MyObject>并选择Implement Interface => Implement Interface,Visual Studio会帮助添加以下代码块:IEnumerator<MyObject> IEnumerable<MyObject>.GetEnumerator(){    throw new NotImplementedException();}这次从该GetEnumerator();方法返回非通用IEnumerable对象不起作用,那么我在这里放什么呢?现在,CLI会忽略非通用实现,并在foreach循环中尝试枚举数组时直接进入通用版本。
查看完整描述

3 回答

?
慕丝7291255

TA贡献1859条经验 获得超6个赞

如果您选择使用通用集合(例如List<MyObject>而不是)ArrayList,则会发现List<MyObject>会同时提供您可以使用的通用和非通用枚举器。


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

    }

}


查看完整回答
反对 回复 2019-11-26
?
慕村225694

TA贡献1880条经验 获得超4个赞

您可能不想要显式的实现IEnumerable<T>(这就是您所显示的)。


通常的模式是使用IEnumerable<T>的GetEnumerator在明确的执行IEnumerable:


class FooCollection : IEnumerable<Foo>, IEnumerable

{

    SomeCollection<Foo> foos;


    // Explicit for IEnumerable because weakly typed collections are Bad

    System.Collections.IEnumerator IEnumerable.GetEnumerator()

    {

        // uses the strongly typed IEnumerable<T> implementation

        return this.GetEnumerator();

    }


    // Normal implementation for IEnumerable<T>

    IEnumerator<Foo> GetEnumerator()

    {

        foreach (Foo foo in this.foos)

        {

            yield return foo;

            //nb: if SomeCollection is not strongly-typed use a cast:

            // yield return (Foo)foo;

            // Or better yet, switch to an internal collection which is

            // strongly-typed. Such as List<T> or T[], your choice.

        }


        // or, as pointed out: return this.foos.GetEnumerator();

    }

}


查看完整回答
反对 回复 2019-11-26
?
慕尼黑8549860

TA贡献1818条经验 获得超11个赞

请注意,另一种已IEnumerable<T>实现的System.Collections方法是MyObjects从类派生您的类System.Collections作为基类(documentation):


System.Collections:提供通用集合的基类。


我们以后可以使我们自己实行重写虚拟System.Collections方法,以提供定制的行为(仅适用于ClearItems,InsertItem,RemoveItem,和SetItem沿Equals,GetHashCode以及ToString从Object)。不同于,List<T>后者的设计不容易扩展。


例:


public class FooCollection : System.Collections<Foo>

{

    //...

    protected override void InsertItem(int index, Foo newItem)

    {

        base.InsertItem(index, newItem);     

        Console.Write("An item was successfully inserted to MyCollection!");

    }

}


public static void Main()

{

    FooCollection fooCollection = new FooCollection();

    fooCollection.Add(new Foo()); //OUTPUT: An item was successfully inserted to FooCollection!

}

请注意,collection仅在需要自定义收集行为的情况下才建议使用这种驱动,这种情况很少发生。见用法。


查看完整回答
反对 回复 2019-11-26
  • 3 回答
  • 0 关注
  • 543 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信