Troubles implementing IEnumerable

后端 未结 7 914
清酒与你
清酒与你 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:47

    Since IEnumerable implements IEnumerable you need to implement this interface as well in your class which has the non-generic version of the GetEnumerator method. To avoid conflicts you could implement it explicitly:

    IEnumerator IEnumerable.GetEnumerator()
    {
        // call the generic version of the method
        return this.GetEnumerator();
    }
    
    public IEnumerator GetEnumerator()
    {
        for (int i = 0; i < Count; i++)
            yield return _array[i];
    }
    

提交回复
热议问题