第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何實現(xiàn)IEnumerable <T>

如何實現(xiàn)IEnumerable <T>

慕尼黑的夜晚無繁華 2019-11-26 15:30:45
我知道如何實現(xiàn)非通用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>,但我不知道如何實現(xiàn)它。如果添加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對象不起作用,那么我在這里放什么呢?現(xiàn)在,CLI會忽略非通用實現(xiàn),并在foreach循環(huán)中嘗試枚舉數(shù)組時直接進入通用版本。
查看完整描述

3 回答

?
慕絲7291255

TA貢獻1859條經(jīng)驗 獲得超6個贊

如果您選擇使用通用集合(例如List<MyObject>而不是)ArrayList,則會發(fā)現(xiàn)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();

    }

}


查看完整回答
反對 回復(fù) 2019-11-26
?
慕村225694

TA貢獻1880條經(jīng)驗 獲得超4個贊

您可能不想要顯式的實現(xiàn)IEnumerable<T>(這就是您所顯示的)。


通常的模式是使用IEnumerable<T>的GetEnumerator在明確的執(zhí)行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();

    }

}


查看完整回答
反對 回復(fù) 2019-11-26
?
慕尼黑8549860

TA貢獻1818條經(jīng)驗 獲得超11個贊

請注意,另一種已IEnumerable<T>實現(xiàn)的System.Collections方法是MyObjects從類派生您的類System.Collections作為基類(documentation):


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


我們以后可以使我們自己實行重寫虛擬System.Collections方法,以提供定制的行為(僅適用于ClearItems,InsertItem,RemoveItem,和SetItem沿Equals,GetHashCode以及ToString從Object)。不同于,List<T>后者的設(shè)計不容易擴展。


例:


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僅在需要自定義收集行為的情況下才建議使用這種驅(qū)動,這種情況很少發(fā)生。見用法。


查看完整回答
反對 回復(fù) 2019-11-26
  • 3 回答
  • 0 關(guān)注
  • 615 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號