title: Implementing IEnumerable for generic tree c#
body: <p>I have a generic tree class, for which I want to implement the IEnumerable interface. The previous questions I found on stackoverflow were a bit different from my problem. I know that I am doing something wring but I don't know what it is. Here is my code:</p>
  
  <pre><code>class Node&lt;T&gt;: IEnumerable&lt;T&gt;
  {
      private T data;
      private LinkedList&lt;Node&lt;T&gt;&gt; children;
  
      public Node(T data)
      {
          this.data = data;
          children = new LinkedList&lt;Node&lt;T&gt;&gt;();
      }
  
      public void AddChildNode(Node&lt;T&gt; node)
      {
          children.AddFirst(node);
      }
  
      public void MyTraverse(Node&lt;T&gt; node, List&lt;T&gt; visited)
      {
          visited.Add(node.data);
          foreach (Node&lt;T&gt; kid in node.children)
              MyTraverse(kid, visited);
  
      }
  
      public IEnumerator&lt;T&gt; GetEnumerator()
      {
          return children.GetEnumerator();
      }
  
  
      System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
      {
          return GetEnumerator();
      }
  }
  </code></pre>
  
  <p>In the function GetEnumerator() I get a casting error which I don't know how to fix. Can any one help me?</p>