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

  • SEARCH
  • Home
  • 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 8032725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:28:21+00:00 2026-06-05T01:28:21+00:00

I am having some trouble making an iterator that can traverse the following type

  • 0

I am having some trouble making an iterator that can traverse the following type of data structure.

I have a class called Expression, which has one data member, a List<object>.

This list can have any number of children, and some of those children might be other Expression objects.

I want to traverse this structure, and print out every non-list object (but I do want to print out the elements of the list of course), but before entering a list, I want to return “begin nest” and after I just exited a list, I want to return “end nest”.

I was able to do this if I ignored the class wherever possible, and just had List<object> objects with List<object> items if I wanted a subExpression, but I would rather do away with this, and instead have an Expressions as the sublists (it would make it easier to do operations on the object. I am aware that I could use extension methods on the List<object> but it would not be appropriate (who wants an Evaluate method on their list that takes no arguments?).

The code that I used to generate the origonal iterator (that works) is:

    public IEnumerator GetEnumerator(){
        return theIterator(expr).GetEnumerator();
    }
    private IEnumerable theIterator(object root) {
        if ((root is List<object>)){
            yield return " begin nest ";
            foreach (var item in (List<object>)root){
                foreach (var item2 in theIterator(item)){
                    yield return item2;
                }
            }
            yield return " end nest ";
        }
        else
            yield return root;
    }

A type swap of List<object> for expression did not work, and lead to a stackOverflow error. How should the iterator be implemented?

Update: Here is the swapped code:

    public IEnumerator GetEnumerator() {
        return this.GetEnumerator();
    }
    private IEnumerable theIterator(object root) {
        if ((root is Expression)) {
            yield return " begin nest ";
            foreach (var item in (Expression)root) {
                foreach (var item2 in theIterator(item))
                    yield return item2;
            }
            yield return " end nest ";
        }
        else
            yield return root;
    }
  • 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-06-05T01:28:24+00:00Added an answer on June 5, 2026 at 1:28 am

    The reason you are getting a StackOverflowException is that

    foreach (var item in (Expression)root)
    

    …internally causes:

    ((Expression)root).GetEnumerator()
    

    …to be invoked – that is the way that the CLR enumerates the objects that must be assigned to the item variable during each iteration of the foreach loop.

    In your case, the GetEnumerator() invocation would cause theIterator to be executed again for the same root, thereby entering an infinite recursion.

    To fix your problem, you need to replace:

    foreach (var item in (Expression)root)
    

    …with:

    foreach (var item in ((Expression)root).expr)
    

    …where expr is the name of the List<object> property.

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

Sidebar

Related Questions

I'm having some trouble with an iPhone app that I'm making. I have two
Im having some trouble getting the syntax right. I have a movie clip that
I´m having some trouble retrieving a collection of strings in a projection: say that
Im having some trouble writing a getstring function, this is what I have so
Im having some trouble with part of a lab for class, most of it
I've created this simple demo because I'm having some trouble making auto-sizing work in
i'm having some trouble in making this on right. I'm trying to get the
I'm having some trouble removing an element that gets added dynamically to the DOM.
I am working on a class library and am having some trouble with generics.
Sorry if this is a basic question - I'm having some trouble making the

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.