title: Performance of nested yield in a tree
body: <p>I've got a tree-like structure. Each element in this structure should be able to return a Enumerable of all elements it is root to. Let's call this method <code>IEnumerable&lt;Foo&gt; GetAll()</code>. So if we have</p>
  <pre><code>     A  &lt;-- topmost root
      / \ 
     /   \
    B     C
   / \   / \
  D   E F   G
  </code></pre>
  <p>a call to <code>GetAll</code> on element <code>C</code> returns <code>{C, F, G}</code> (fixed order of elements would be nice, but is not needed). I guess everybody knew that already.</p>
  <p>The current implementation of <code>GetAll</code> looks like this:</p>
  <pre><code>public IEnumerable&lt;Foo&gt; GetAll ()
  {
      yield return this;
  
      foreach (Foo foo in MyChildren) {
          foreach (Foo f in foo.GetAll ()) {
              yield return f;
          }
      }
  }
  </code></pre>
  <p>In an earlier implementation, I returned a List and added the child-foos using <code>List.AddRange()</code>.</p>
  <p>My question is if the version using yield is correcly implemented or if it should be improved (esp. in terms of performance). Or is this just bad and I should stick to <code>List</code>s (or <code>ReadOnlyCollections</code>) instead?</p>