使用yield关键字,非常的方便
private static IEnumerable<Control> GetChildren(Control frmRootDock) { if (frmRootDock == null) { throw new ArgumentNullException(""); } var list = new List<Control>(); foreach (Control item in frmRootDock.Controls) { foreach (Control c in GetChildren(item)) { yield return c; } yield return item; } }