-1

I need some help in creating a hierarchical data structure menu items in console application using .NET Core.

I want the structure to be similar to below.

Parent

  • Child 1 --Another Child 1A --Another Child 2A --- Another Sub Child 1A
  • Child 2 --Another Child 1B
TylerH
  • 20,799
  • 66
  • 75
  • 101
VAM
  • 1
  • Can I request for an example help please – VAM Jul 17 '20 at 20:41
  • What you're looking for is a Tree structure, I guess. There's a million implementations of trees already in the wild. Find one that fits your needs. – Isaí Hinojos Jul 17 '20 at 20:44
  • Can you please point at one example to get a basic idea? I am struggling to find a good example – VAM Jul 17 '20 at 20:45
  • 1
    I'm afraid that I can't, there's not a 'one size fits all' approach for trees, but you can get a grasp of what a tree can be like in here : https://stackoverflow.com/questions/66893/tree-data-structure-in-c-sharp – Isaí Hinojos Jul 17 '20 at 20:53
  • I will take a look at the examples and will try to come out with an answer. Thank you for taking time. Appreciate it. – VAM Jul 17 '20 at 21:13

2 Answers2

0

You can start with this:

  public class Child
  {
      public List<Child> Children { get; set; }
  }

And if you want some sort of tree structure:

  var child1 = new Child() 
  { 
      Children = new List<Child>
           new Child(),
           new Child() { Children = new List<Child> { new Child() } },
           new Child()            
  };

Or this

  var child1 = new Child();
  var child2 = new Child();
  var child3 = new Child();
  var child4 = new Child();
  var child5 = new Child();

  child1.Children = new List<Child>{ child2, child3 };
  child3.Children = new List<Child> { child4, child5 };

Please note that there might be data integrity issues since List is exposed as public, meaning anybody can change Children or its contents, but you can look at that later.

0

You can also do;

public class Child{
 public int Id {get; set;}
 public Child subChild{get; set;}
}
Baturhan
  • 31
  • 4
  • I think this is what I am doing. Will paste the exact answer once I am done. Thank you. – VAM Jul 18 '20 at 22:22