Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this Recursive or Iteration?

if you look it is calling the method FindChildControls in the bottom (inner)foreach statement, since it is coming from a foreach does that make it recursive or iterative?

Thanks!

public static IEnumerable<T> FindChildControls<T>(this ControlCollection controlCollection) where T: class 
{
  foreach(Control control in controlCollection)
  {
    if(control is T)
    {
      yield return control as T;  
    }
    foreach(T type in control.Controls.FindChildControls<T>())  
    {  
      yield return type;  
    }  
  }   
} 
like image 598
Jordan Avatar asked Dec 01 '22 04:12

Jordan


1 Answers

This method is recursive because it calls itself on line 9. It also uses iterations (foreach loops). It's also lazy as it yields results, so unless the caller loops through the enumerator nothing will execute.

like image 61
Darin Dimitrov Avatar answered Dec 05 '22 08:12

Darin Dimitrov