title: IEnumerable and Recursion using yield return
body: <p>I have an <code>IEnumerable&lt;T&gt;</code> method that I'm using to find controls in a WebForms page.</p>
  <p>The method is recursive and I'm having some problems returning the type I want when the <code>yield return</code> is returnig the value of the recursive call.</p>
  <p>My code looks as follows:</p>
  <pre><code>public static IEnumerable&lt;Control&gt; GetDeepControlsByType&lt;T&gt;(this Control control)
  {
      foreach(Control c in control.Controls)
      {
          if (c is T)
          {
              yield return c;
          }
  
          if(c.Controls.Count &gt; 0)
          {
              yield return c.GetDeepControlsByType&lt;T&gt;();
          }
      }
  }
  </code></pre>
  <p>This currently throws a &quot;Cannot convert expression type&quot; error. If however this method returns type <code>IEnumerable&lt;Object&gt;</code>, the code builds, but the wrong type is returned in the output.</p>
  <p>Is there a way of using <code>yield return</code> whilst also using recursion?</p>