Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IEnumerable and Recursion using yield return

I have an IEnumerable<T> method that I'm using to find controls in a WebForms page.

The method is recursive and I'm having some problems returning the type I want when the yield return is returnig the value of the recursive call.

My code looks as follows:

public static IEnumerable<Control> GetDeepControlsByType<T>(this Control control) {     foreach(Control c in control.Controls)     {         if (c is T)         {             yield return c;         }          if(c.Controls.Count > 0)         {             yield return c.GetDeepControlsByType<T>();         }     } } 

This currently throws a "Cannot convert expression type" error. If however this method returns type IEnumerable<Object>, the code builds, but the wrong type is returned in the output.

Is there a way of using yield return whilst also using recursion?

like image 814
Jamie Dixon Avatar asked Jan 13 '10 10:01

Jamie Dixon


2 Answers

Inside a method that returns IEnumerable<T>, yield return has to return T, not an IEnumerable<T>.

Replace

yield return c.GetDeepControlsByType<T>(); 

with:

foreach (var x in c.GetDeepControlsByType<T>()) {   yield return x; } 
like image 136
Marcin Seredynski Avatar answered Sep 28 '22 10:09

Marcin Seredynski


You need to yield each of the items yielded by the recursive call:

public static IEnumerable<Control> GetDeepControlsByType<T>(this Control control) {     foreach(Control c in control.Controls)     {         if (c is T)         {             yield return c;         }          if(c.Controls.Count > 0)         {             foreach (Control control in c.GetDeepControlsByType<T>())             {                 yield return control;             }         }     } } 

Note that there's a cost to recursing in this way - you'll end up creating a lot of iterators, which can create a performance issue if you have a really deep control tree. If you want to avoid that, you basically need to do the recursion yourself within the method, to make sure there's only one iterator (state machine) created. See this question for more details and a sample implementation - but this obviously adds a certain amount of complexity too.

like image 29
Jon Skeet Avatar answered Sep 28 '22 08:09

Jon Skeet