Troubles implementing IEnumerable

后端 未结 7 913
清酒与你
清酒与你 2020-11-29 11:08

I\'m trying to write my own (simple) implementation of List. This is what I did so far:

using System;
using System.Collections.Generic;
using System.Linq;
us         


        
7条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 11:33

    Since your class, MyList inherits IEnumerable which in turn inherits the non-generic IEnumerable, you have two methods you need to implement:

    IEnumerator IEnumerable.GetEnumerator()
            {
                throw new NotImplementedException();
            }
    
            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                throw new NotImplementedException();
            }
    

    An easy way to do this is when you declare your class:

    class MyList : IEnumerable
    

    Right click the IEnumerable text and select Implement Interface > Implement Interface Explictly from the context menu.

提交回复
热议问题