Why doesn't the following work? (IEnumerable/ IEnumerator)

virtualmic

I was trying out the example on this MSDN page. I tried to change the GetEnumerator method. I know something doesn't seem right in that, but it complies and then doesn't run. Error is that the Enumerator has not started and that MoveNext should be called, but it is being called!

class Program
{
    static void Main(string[] args)
    { 
        foreach (var day in new DaysOfTheWekk())
        {
            Console.WriteLine(day) ;
        }
        Console.ReadLine();
    }
}

public class DaysOfTheWekk: IEnumerable
{
    private string[] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

    public IEnumerator GetEnumerator()
    {
        days.GetEnumerator().MoveNext();
        yield return days.GetEnumerator().Current;
    }
}
Matten

Why do you want to call moveNext? Just leave out the .Current:

public class DaysOfTheWeek: IEnumerable
{
    private string[] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

    public IEnumerator GetEnumerator()
    {
        return days.GetEnumerator();
    }
}

Otherwise use a while loop since:

public class DaysOfTheWeek: IEnumerable
{
    private string[] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

    public IEnumerator GetEnumerator()
    {
        var enumerator = days.GetEnumerator();
        while(enumerator.MoveNext())
        { 
            yield return enumerator.Current;
        }
    }
}

Explanation: The GetEnumerator() method always returns a new enumerator, so if you call GetEnumerator().Current, then the MoveNext() function has not been called on the newly returned instance! Use a variable as stated in my second example, instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is IEnumerable<T> necessary when there is IEnumerator<T>?

From Dev

The following code doesn't work .. why?

From Dev

The following code doesn't work .. why?

From Dev

Why doesn't the following statement work?

From Dev

Why doesn't the following query work with double LIKE clause?

From Dev

Why doesn't the following, very simple, liquid markup work?

From Dev

Why NVL() doesn't work in the following outer join(+)?

From Dev

Why timezone in angular doesn't work as expected in the following case

From Dev

Recursive IEnumerable doesn't work as expected?

From Dev

IEnumerable<T> and IEnumerator - some clarification please

From Dev

IEnumerable<T> and IEnumerator - some clarification please

From Dev

Why doesn't that work?

From Dev

Why doesn't a ListBox bound to an IEnumerable update?

From Dev

Why doesn't following the Fabric.IO steps for Twitter Login work for Android apps?

From Dev

Why doesn't the following html and css hover code work when I add additional hyperlink within a div?

From Dev

Why does the following code works with one model but doesn't work with another?

From Dev

Why following code doesn't compile?

From Dev

why the following code doesn't perform as expected?

From Dev

Why following code doesn't compile?

From Dev

Why doesn't OFS work?

From Dev

Why doesn't strcpy work?

From Dev

Why doesn't ${#$2} work?

From Dev

Why doesn't this closure work?

From Dev

Why stylesheet doesn't work?

From Dev

Why fadeIn doesn't work?

From Dev

Why doesn't sleep work?

From Dev

Why isEOF doesn't work?

From Dev

Why @PostConstruct doesn't work?

From Dev

Why doesn't the keylistener work?

Related Related

HotTag

Archive