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 6709445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:52:58+00:00 2026-05-26T07:52:58+00:00

I have a recursive method that builds a tree-like structure of a resource and

  • 0

I have a recursive method that builds a tree-like structure of a resource and its related resources.

For each resource that I work with I add it to a class member list which I check on each iteration to make sure that we don’t loop infinately on resources that are inter dependent.

Each time I call this recursive method for the first time, I need the class member list to be clear.

At the moment I have a seperate method to do this which I can call between calls to the recursive method.

I’d like to get rid of this method call and reset the list automatically each time.

At the moment I can see two options to solve this problem:

  1. Test whether the calling method is the same as the currently
    executing method and if not, reset the list
  2. Get rid of the recursion and queue items instead, dequeueing and
    enqueueing as we go. At the end of the method call I can reset the list.

How would you go about solving this problem? What approach would you take?

Here’s how my code currently looks:

public class GetAllRelatedResourcesByParentGuidQuery : IGetAllRelatedResourcesByParentGuidQuery
    {
        private readonly IList<Guid> _itemsCheckedForRelations = new List<Guid>();

        public IEnumerable<IDependency> Invoke(Guid parentCiId, 
                                                  IResoucesByIdQuery getResources)
        {
            if (!_itemsCheckedForRelations.Contains(parentCiId))
            {
                var relatedResources = getResources.Invoke(parentCiId);

                _itemsCheckedForRelations.Add(parentCiId);

                if (relatedResources.Count() > 0)
                {
                    foreach (var relatedResource in relatedResources)
                    {
                        relatedResource.Resource.DependentResources = Invoke(
                                                         relatedResource.Resource.Id, 
                                                         getResources);

                        yield return relatedResource;
                    }
                }
            }
        }

        public void ResetCheckedItemsCollection()
        {
            _itemsCheckedForRelations.Clear();
        }
    }
  • 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-26T07:52:58+00:00Added an answer on May 26, 2026 at 7:52 am

    I would make a public method which performs the creation, but make the recursive method not care, and take it as a parameter.

    public List<string> DoSomething(int input)
    {
        List<string> results = new List<string>();
        DoSomethingImpl(input, results);
        return results;
    }
    
    private void DoSomethingImpl(int input, List<T> results)
    {
        // For example...
        if (input == 0)
        {
            return results;
        }
        results.Add("Foo");
        DoSomethingImpl(input - 1, results);        
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Welcome! I have a recursive public static method named less that takes a tree
I have a recursive method that returns categories, and checks for its sub categories.
I have a recursive method on the base form that takes in a control
We have a dynamic languaging system that doesn't use conventional resource files; fetching resources
I have an entity that is actually tree object structure (defining only relevant properties
I have a recursive call to a method that throws a stack overflow exception.
I have a recursive method call. When any exception is thrown, I would like
I have a rather annoying issue that I solved using a simple recursive method
I have a MVC application which has a Controller that has a recursive method
I have a recursive method that reversed a string (HW assignment, has to be

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.