Structure of a nested object hierarchy

I have an object that can have parents and children of the same type of object. Is there a standardized way to implement this template?

Sort of:

public class Nested { public string Name {get; set;} public List<Nested> Parents {get {TODO}} public List<Nested> Children {get {TODO}} } 
+4
source share
2 answers

Look at the compound template here

+4
source

From a memory point of view, it seems that any instance of your class will have to filter everything recursively to figure out what the relationship is. It is burdensome.

I would recommend just giving your class a property similar to this (or something more complex, depending on what you want to accomplish):

 public Nested parent; 

This will turn it into a linked list. You can separately create a method outside the class that manages to find parents based on a limited set of parameters (for example, FindParentsOfNestedToCertainGeneration (int numOfGenerations, Nested child)). It will simply pass on to the child and raise the chain of parents to the β€œparent” property.

+1
source

All Articles