title: Implement a parent-child class hierarchy
body: <p>I'm finding it difficult to find a decent example on how to implement a parent-child hierarchy class.
  I have a treeView control that I want to convert into a class hierarchy, adding extra data to each node and be able to easely iterate over each parent's nodes using IEnumerable.</p>
  
  <pre><code>public IEnumerable&lt;Node&gt; GetAllChildsFromParent(Node parent)
  {
      foreach (Node node in parent.NodeChildsCollection)
      {
          yield return node;
      }
  }
  </code></pre>
  
  <p>I already have implemented the following piece of code but got stuck and don't really
  have a clue whether I am on the right track or not? How should I proceed to complete this ?</p>
  
  <pre><code>public class NodeChildsCollection : IEnumerable&lt;Node&gt;
  {
      IList&lt;Node&gt; nodeCollection = new List&lt;Node&gt;();
      Node parent;
  
      public Node Parent
      {
          get { return parent; }
          set { parent = value; }
      }
  
      public NodeChildsCollection()
      {
      }
  
  
      public void AddNode(Node parent, Node child)
      {
          this.parent = parent;
          nodeCollection.Add(child);
      }
  
      #region IEnumerable&lt;Node&gt; Members
  
      public IEnumerator&lt;Node&gt; GetEnumerator()
      {
          foreach (Node node in nodeCollection)
          {
              yield return node;
          }
      }
  
      #endregion
  
      #region IEnumerable Members
  
      IEnumerator IEnumerable.GetEnumerator()
      {
          return GetEnumerator();
      }
  
      #endregion
  }
  
  public class Node
  {
  
      NodeChildsCollection nodeChildsCollection = new NodeChildsCollection();
  
      public Node Parent
      {
          get { return nodeChildsCollection.Parent; }
          set { nodeChildsCollection.Parent = value; }
      }
  
  
      public void AddChild(Node child)
      {
          nodeChildsCollection.AddNode(this, child);
      }
  }
  </code></pre>