Doing IEnumerator.MoveNext ()?

I'm kind of new to C #, I know that the method defined in the interface needs to be implemented

but in the code below I did not implement the MoveNext () method

static void Main()
{
    List<int> list = new List<int>();
    list.Add(1);
    list.Add(5);
    list.Add(9);

    List<int>.Enumerator e = list.GetEnumerator();
    Write(e);
}

static void Write(IEnumerator<int> e)
{
    while (e.MoveNext())
    {
        int value = e.Current;
        Console.WriteLine(value);
    }
}

I also checked in the metadata and it does not provide any implementation.

so why doesn't the compiler throw any error? where is the MoveNext () method implemented & how does it go to the next value?

Is the code for the MoveNext () method an automatically generated compiler? please, help

+4
source share
1 answer

The reason is that you did not implement IEnumerator<int>, you used the List<int>one that implemented it, and provided the implementation for MoveNext.

:

public bool MoveNext() {

    List<T> localList = list;

    if (version == localList._version && ((uint)index < (uint)localList._size)) 
    {                                                     
        current = localList._items[index];                    
        index++;
        return true;
    }
    return MoveNextRare();
}
+5

All Articles