开发者

.NET IEnumerator<string> not advancing on MoveNext when in yield block

The code below (for running in LinqPad) is meant to parse the "foo/skip/bar" string into item objects, skipping over the 'skip' bit, yielding Item objects for "foo" and "bar". When run, 2 "bar" items are produced.

In the TryGetChild method, when "skip" is found, the enumerator is moved from "skip" forward to "bar". However, when execution returns into the calling method, the enumerator is back on "skip".

I think this is some yield block weirdness, as if I do the split in Main() and pass the enumerator into Walk() it works properly. Can someone explain how the enumerator goes back? Is a new one being created?

edit: This is a very simplified version of the seemingly odd situation I have found in my code. I am asking this question out of inquisitiveness rather than looking for a workaround, which I have already found.

/* output from program

enumerator moved to foo
enumerator moved to skip
enumerator moved to bar
enumerator moved to bar

Item [] (3 items)  

foo
bar     
bar
*/     


static void Main()
{
    Walk("foo/skip/bar").ToArray().Dump()开发者_如何学C;
}

private static IEnumerable<Item> Walk(string pathString)
{
    var enumerator = pathString.Split('/').ToList().GetEnumerator();
    var current = new Item() { S = "" };
    while (enumerator.MoveNext())
    {
        Console.WriteLine("enumerator moved to " + enumerator.Current);
        yield return current.TryGetChild(enumerator);
    }
}
class Item
{
    public string S { get; set; }

    public Item TryGetChild(IEnumerator<string> enumerator)
    {
        if (enumerator.Current == "skip")
        {
            enumerator.MoveNext(); //iterator moves on to 123
            Console.WriteLine("enumerator moved to " + enumerator.Current);
        }
        return new Item() { S = enumerator.Current };
    }
}


The reason you see this behaviour is that List<T>.GetEnumerator() returns an instance of List<T>.Enumerator, which is a struct. Thus you are passing a value type to the TryGetChild() method, and any mutations on that type (including those done by MoveNext()) will not be reflected in the caller.


I think yield only works when using an IEnumerable or IEnumerable<T> loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜