Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6367645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:34:33+00:00 2026-05-25T00:34:33+00:00

The code below (for running in LinqPad) is meant to parse the foo/skip/bar string

  • 0

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();
}

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 };
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:34:33+00:00Added an answer on May 25, 2026 at 12:34 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running the below code to parse an HTML link using HTML
I'm running into a problem with method overwriting. Look at the src code below,
When running the below code a type is never returned, despite there being a
Hello friends i am running code given below which contains the setLogTimeEntery function and
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
The code below only prints one instance id per instance even though I'm running
I get this message when running the code below: There is an error in
I have the code below running in my application and it takes approx 2
I'm running the code below and getting an empty chart using Flot/jQuery. Ideally what
I'm having trouble running the below code on an Oracle DB. Not too sure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.