You can do:
public static void ForAllChildren(Action<Control> action, params Control[] parents) { foreach(var p in parents) foreach(Control c in p.Controls) action(c); }
Called as:
ForAllChildren(x => Foo(x), tb_Invoices, tb_Statements);
You may have suffered a little from performance for invoking an action, although in this case you could just use a nested foreach :
foreach (var p in new Control[] { tb_Invoices, tb_Statements }) foreach (Control c in p.Controls) Foo(c);
Similarly, a general solution for cyclic switching all elements in any nonequivalent IEnumerable could be (although a bit like using a sledgehammer to drive a nail):
public static void ForEachAll<T>(Action<T> action, params System.Collections.IEnumerable[] collections) { foreach(var collection in collections) foreach(var item in collection.Cast<T>()) action(item); }
Called as:
ForEachAll<Control>(x => Foo(x), tb_Invoices.Controls, tb_Statements.Controls);
source share