亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何實現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 關注
  • 607 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號