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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:38:19+00:00 2026-06-08T09:38:19+00:00

I have the following class which recurs on itself to form a tree-like data

  • 0

I have the following class which recurs on itself to form a tree-like data structure:

public class chartObject
{
    public string name { get; set; }
    public int descendants { get; set; }
    public List<chartObject> children { get; set; }
}

For each object in the tree I would like to populate the descendant property with the amount objects that exist underneath it.

Example structure:

chartObject1 (descendants: 4)
└-chartObject2 (descendants: 0)
└-chartObject3 (descendants: 2)
└--chartObject4 (descendants: 1)
└---chartObject5 (descendants: 0)

What would be the most efficient way of doing this?

  • 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-08T09:38:22+00:00Added an answer on June 8, 2026 at 9:38 am

    This works for me:

    public void SetDescendants(chartObject current)
    {
        foreach (var child in current.children)
        {
            SetDescendants(child);
        }
        current.descendants = current.children.Sum(x => 1 + x.descendants);
    }
    

    I tested with this code:

    var co = new chartObject()
    {
        name = "chartObject1",
        children = new List<chartObject>()
        {
            new chartObject()
            {
                name = "chartObject2",
                children = new List<chartObject>() { }
            },
            new chartObject()
            {
                name = "chartObject3",
                children = new List<chartObject>()
                {
                    new chartObject()
                    {
                        name = "chartObject4",
                        children = new List<chartObject>()
                        {
                            new chartObject()
                            {
                                name = "chartObject5",
                                children = new List<chartObject>() { }
                            }
                        }
                    }
                }
            }
        }
    };
    

    And got this as the result:

    result

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

Sidebar

Related Questions

I have the following classes class A{ private String name; private int value; public
I have the following class: public class PostCode { public string Name { get;
I have a class which must implement the following property public ICollection<IType> Items {
I have the following abstract class which CAN NOT be changed. public abstract class
I have the following C# helper class which retrieves a Json String from a
I have the following class in Java which prints Hello World in portuguese: public
I have the following class (which is a JPA entity listener): @Service public class
Hi I have the following class which provides access to a xml file: public
Lets say i have the following scenario: public class A { public String createString(final
Let's suppose that I have the following class which tries to be immutable public

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.