Last active
December 14, 2015 18:09
-
-
Save PeterStegnar/5127896 to your computer and use it in GitHub Desktop.
Stackoverlow.com answer to the question: http://stackoverflow.com/questions/15318438/building-a-tree-menu-with-recursion-in-mvc4-which-data-structure-to-use
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace SO.15318438 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
BoxNode boxNode = new BoxNode(){BoxChildren = new List<Box>()}; | |
boxNode.BoxChildren.Add(new Box(){BoxNode = null, Name = "1Level1_0C" }); | |
boxNode.BoxChildren.Add(new Box() { BoxNode = | |
new BoxNode() { BoxChildren = new List<Box>() { new Box() { BoxNode = null, Name = "2Level1_0C" }, new Box() { BoxNode = null, Name = "2Level2_0C" } } }, | |
Name = "1Level2_2C" }); | |
CreateMenuTree(boxNode, " "); | |
Console.ReadLine(); | |
} | |
public static void CreateMenuTree(BoxNode boxNode, string indent) | |
{ | |
foreach (Box box in boxNode.BoxChildren) | |
{ | |
Console.WriteLine(indent + box.Name); | |
if (box.BoxNode != null && box.BoxNode.BoxChildren != null && box.BoxNode.BoxChildren.Count > 0) | |
{ | |
CreateMenuTree(box.BoxNode, indent + indent); | |
} | |
} | |
} | |
} | |
public class BoxNode | |
{ | |
public List<Box> BoxChildren | |
{ | |
get; | |
set; | |
} | |
} | |
public class Box | |
{ | |
public string Name | |
{ | |
get; | |
set; | |
} | |
public BoxNode BoxNode | |
{ | |
get; | |
set; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment