
Lambda is like sudo make me sandwich: ^_^
string prefix = "tm";
this.GetAllControls().OfType<CheckBox>()
.Where(cx => cx.Name.Substring(0, 2) == prefix)
.ForEach(cx => cx.Visible = false); // do something here in ForEach
The above code reads as: prepare me a list of checkbox which starts with letters tm, and i'll do something on those checkboxes. You can concentrate on things you want to do.
I'll make it myself(without lambda):
string prefix = "tm";
foreach(Control cx in this.GetAllControls())
{
if (cx is CheckBox && cx.Name.Substring(0, 2) == prefix)
{
// payload here, er.. do something here
(cx as CheckBox).Visible = false;
}
// who can be possibly sure that prefix wasn't altered here
// prefix = "ax";
}
The above code reads as: i'm looking at all controls, check if control is checkbox and starts with letters tm, then i'll do something on that checkbox. And because it is loop, programmers tend to re-read the loop header's condition if all corner cases are covered.
