<p>Can someone show me how to implement a recursive lambda expression to traverse a tree structure in C#. </p>

[{'answer_id': 61149, 'author': 'DevelopingChris', 'author_id': 1220, 'author_profile': 'https://Stackoverflow.com/users/1220', 'pm_score': 1, 'selected': False, 'text': '<p>Assuming a mythical object TreeItem, that conatins a Children collection to represent your hierarchy.</p>\n\n<pre><code>    public void HandleTreeItems(Action&lt;TreeItem&gt; item, TreeItem parent)\n    {\n        if (parent.Children.Count &gt; 0)\n        {\n            foreach (TreeItem ti in parent.Children)\n            {\n                HandleTreeItems(item, ti);\n            }\n        }\n\n        item(parent);\n    }\n</code></pre>\n\n<p>Now to call it, passing in the lambda that handles one item, by printing its name to the console.</p>\n\n<pre><code>HandleTreeItems(item =&gt; { Console.WriteLine(item.Name); }, TreeItemRoot);\n</code></pre>\n'}, {'answer_id': 61206, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 5, 'selected': False, 'text': '<p>A proper solution, and indeed the idiomatic solution in many functional programming languages, would be the use of a <a href="https://en.wikipedia.org/wiki/Fixed-point_combinator" rel="nofollow noreferrer">fixed-point combinator</a>. In a nutshell: a fixed-point combinator answers the question “how do I define an anonymous function to be recursive?”. But the solution is so nontrivial that whole articles are written to explain them.</p>\n\n<p>A simple, pragmatic alternative is to “go back in time” to the antics of C: declaration before definition. Try the following (the “factorial” function):</p>\n\n<pre><code>Func&lt;int, int&gt; fact = null;\nfact = x =&gt; (x == 0) ? 1 : x * fact(x - 1);\n</code></pre>\n\n<p>Works like a charm.</p>\n\n<p>Or, for a pre-order tree traversal on an object of class <code>TreeNode</code> which implements <code>IEnumerable&lt;TreeNode&gt;</code> appropriately to go over its children:</p>\n\n<pre><code>Action&lt;TreeNode, Action&lt;TreeNode&gt;&gt; preorderTraverse = null;\npreorderTraverse = (node, action) =&gt; {\n    action(node);\n    foreach (var child in node) preorderTraverse(child, action);\n};\n</code></pre>\n'}, {'answer_id': 61244, 'author': 'Tom Lokhorst', 'author_id': 2597, 'author_profile': 'https://Stackoverflow.com/users/2597', 'pm_score': 4, 'selected': False, 'text': '<blockquote>\n  <p>A simple alternative is to “go back in time” to the antics of C and C++: declaration before definition. Try the following:</p>\n\n<pre><code>Func&lt;int, int&gt; fact = null;\nfact = x =&gt; (x == 0) ? 1 : x * fact(x - 1);\n</code></pre>\n  \n  <p>Works like a charm.</p>\n</blockquote>\n\n<p>Yes, that does work, with one little caveat. C# has mutable references. So make sure you don\'t accidentally do something like this:</p>\n\n<pre><code>Func&lt;int, int&gt; fact = null;\nfact = x =&gt; (x == 0) ? 1 : x * fact(x - 1);\n\n// Make a new reference to the factorial function\nFunc&lt;int, int&gt; myFact = fact;\n\n// Use the new reference to calculate the factorial of 4\nmyFact(4); // returns 24\n\n// Modify the old reference\nfact = x =&gt; x;\n\n// Again, use the new reference to calculate\nmyFact(4); // returns 12\n</code></pre>\n\n<p>Of course, this example is a bit contrived, but this could happen when using mutable references. If you use the combinators from <a href="https://stackoverflow.com/questions/61143/recursive-lambda-expression-to-traverse-a-tree-in-c#61162">aku</a>\'s links, this won\'t be possible.</p>\n'}, {'answer_id': 61257, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 7, 'selected': True, 'text': '<p>Ok, I found some free time finally.<br>\nHere we go:  </p>\n\n<pre><code>class TreeNode\n{\n    public string Value { get; set;}\n    public List&lt;TreeNode&gt; Nodes { get; set;}\n\n\n    public TreeNode()\n    {\n        Nodes = new List&lt;TreeNode&gt;();\n    }\n}\n\nAction&lt;TreeNode&gt; traverse = null;\n\ntraverse = (n) =&gt; { Console.WriteLine(n.Value); n.Nodes.ForEach(traverse);};\n\nvar root = new TreeNode { Value = "Root" };\nroot.Nodes.Add(new TreeNode { Value = "ChildA"} );\nroot.Nodes[0].Nodes.Add(new TreeNode { Value = "ChildA1" });\nroot.Nodes[0].Nodes.Add(new TreeNode { Value = "ChildA2" });\nroot.Nodes.Add(new TreeNode { Value = "ChildB"} );\nroot.Nodes[1].Nodes.Add(new TreeNode { Value = "ChildB1" });\nroot.Nodes[1].Nodes.Add(new TreeNode { Value = "ChildB2" });\n\ntraverse(root);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/61143', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5360/']