简体   繁体   中英

Hierarchical Data Structure Menu in C# Console Application .Net core

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

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.

You can also do;

public class Child{
 public int Id {get; set;}
 public Child subChild{get; set;}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM