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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:26:04+00:00 2026-06-11T23:26:04+00:00

I am using c# and List collection and loaded the values. Once it is

  • 0

I am using c# and List collection and loaded the values. Once it is done I am trying to read them recursively but some how I am not able to achieve this.

the following is my main code.

    private static void Main(string[] args)
            {
                var node = new Node
                    {
                        Name = "N1",
                        Nodes =
                            new List<Node>
                                {
                                    new Node { Name = "N1a" },
                                    new Node { Name = "N1b", Nodes = new List<Node> { new Node { Name = "N1B1" } } },
                                    new Node
                                        {
                                            Name = "N1c",
                                            Nodes =
                                                new List<Node> { new Node { Name = "N1C1", Nodes = new List<Node> {new Node{Name = "N1C1A"} } } }
                                        }
                                }
                    };
                GetNodes( node );
                Console.ReadLine();
            }

 public class Node
        {
            public string Name { get; set; }

            public IList<Node> Nodes { get; set; }
        }

and the function call is following

public static IEnumerable<Node> GetNodes(Node node)
        {
            if (node == null)
            {
                return null;
            }

            Console.WriteLine(node.Name);

            foreach (var n in node.Nodes)
            {
                return GetNodes(n);
            }

            return null;
        }
    }  

Could any one please help me to fix the recursive function?

  • 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-11T23:26:05+00:00Added an answer on June 11, 2026 at 11:26 pm

    If you just want to print the names of all the nodes,

    public static void GetNodes(Node node)
    {
        if (node == null)
        {
            return;
        }
        Console.WriteLine(node.Name);
        foreach (var n in node.Nodes)
        {
            GetNodes(n);
        }
    }
    

    If you want to flatten the tree,

    public static IEnumerable<Node> GetNodes(Node node)
    {
        if (node == null)
        {
            yield break;
        }
        yield return node;
        foreach (var n in node.Nodes)
        {
            foreach(var innerN in GetNodes(n))
            {
                yield return innerN;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a <% foreach ...%> loop to list a collection of items
I am using a asp.net drop down list and it's pulling in a collection
I am trying to sort a Vector list using the Java Collections frameworks. This
I'm using JSF/Facelets, and I'm trying to iterate over some Document objects (custom object)
I am trying to serialize some Linq objects using this code. private byte[] GetSerializedObj(object
How can I fill a table with items in a list collection(item: Year), using
HI all I have problem when trying to convert list collection string to one
good stuff // ok to alias a List Type using AliasStringList = System.Collections.Generic.List<string>; //
I'm using this code: Dim cListItems As New System.Collections.Generic.List(Of Combobox_values) cListItems.Add(New Combobox_values(One, 1)) cListItems.Add(New
when using this method public List<Field> getFieldWithoutId(List<Integer> idSections) throws Exception { try { Connection

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.