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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:15:13+00:00 2026-05-11T08:15:13+00:00

I have a Tree. class TreeNode { public TreeNode(string name, string description) { Name

  • 0

I have a Tree.

class TreeNode {     public TreeNode(string name, string description) {         Name = name;         Description = description;     }     string Name { get; set; }     string Description { get; set; }     public List<TreeNode> Children = new List<TreeNode>(); } 

I would like to populate a large one for unit testing purposes. I would really like to keep stuff DRY.

Say for illustration purposes my tree has the following structure

 Parent,desc    Child 1, desc1     Grandchild 1, desc1    Child 2, desc2 

How would you go about populating the tree in an elegant an maintainable way?

I find this code quite repetitious and error prone:

var parent = new TreeNode('Parent', 'desc'); var child1 = new TreeNode('Child 1', 'desc1'); var child2 = new TreeNode('Child 2', 'desc2'); var grandchild1 = new TreeNode('Grandchild 1', 'desc1');  parent.Children.Add(child1); parent.Children.Add(child2);  child1.Children.Add(grandchild1); 

EDIT

I ended up doing the DSL approach:

A demo test lives here.

The implementation is here.

It uses a builder and a simple DSL.

  • 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. 2026-05-11T08:15:13+00:00Added an answer on May 11, 2026 at 8:15 am
    • Ideally you want a way to extend the language to literals of custom types. C# doesn’t have this, so you have to find another approach.

    • You can fabricate an internal DSL, typically with a fluent interface.

    • Follow the XElement example of functional construction.

    • Create an external DSL with a custom parser. If you design the language carefully, the parser can be easy.

    • Use XML. Basically this is a way to create an external DSL and get the parser for free.

    The external DSL options are nice because when you read them, you know there’s only data, and don’t have to worry about making sense of code constructs. Also, the data is the file, and the file is the data. That makes it easy to swap data around by changing files, and easier to ready file change histories. Finally, and external DSL is good when a non-programmer will be supplying the data.

    The tradeoff here is time vs. value. How much data you will have / how often it will change / who will change it are the questions you have to answer.

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

Sidebar

Ask A Question

Stats

  • Questions 169k
  • Answers 169k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer require 'time' x = "2009/04/16 19:52:30" begin y = Time.parse(x)… May 12, 2026 at 1:51 pm
  • Editorial Team
    Editorial Team added an answer Actually, I think there is a way. The Script Control… May 12, 2026 at 1:51 pm
  • Editorial Team
    Editorial Team added an answer One possibility may be to let customers select subsets (some/all/none)… May 12, 2026 at 1:51 pm

Related Questions

I have created my own Tree implementation for various reasons and have come up
I require a tree / directed acyclic graph implementation something like this: public class
i have a class based on TInterfacedObject. i add it to TTreeNode's Data property.
I'm trying to figure out how to implement a function in a tree node

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.