Jak zaimplementować IEnumerable <T>

Wiem, jak zaimplementować nieogólne IEnumerable, jak poniżej:

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

Zauważam jednak, że IEnumerable ma wersję ogólną,IEnumerable<T>, ale nie wiem, jak go wdrożyć.

Jeśli dodamusing System.Collections.Generic; do moich używających dyrektyw, a następnie zmień:

class MyObjects : IEnumerable

do:

class MyObjects : IEnumerable<MyObject>

A następnie kliknij prawym przyciskiem myszyIEnumerable<MyObject> i wybierzImplement Interface => Implement Interface, Visual Studio pomaga dodać następujący blok kodu:

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

Zwracanie nietypowego obiektu IEnumerable zGetEnumerator(); metoda nie działa tym razem, więc co tu umieściłem? Interfejs CLI ignoruje teraz implementację nietypową i kieruje się prosto do wersji ogólnej, gdy próbuje wyliczyć przez moją tablicę podczas pętli foreach.

questionAnswers(6)

yourAnswerToTheQuestion