How can I restart a foreach
loop in C#??
For example:
Action a;
foreach(Constrain c in Constrains)
{
if(!c.Allows(a))
{
a.Change
Although a very old thread - none of the answers paid due attention to the semantics of that code:
a
a
breaks any of them, try another a
and push that through the chain.That is, a.Change()
should be separated from the constraint checking loop, also adhering to the CQS principle:
while (!MeetsConstraints(a))
{
a.Change();
}
bool MeetsConstraints(Thing a)
{
return Constraints.All(c => c.Allows(a));
}
No goto, no ugly loops, just simple and clean.