问题描述
我怎样才能重新启动的foreach
循环在C#??
How can I restart a foreach
loop in C#??
例如:
Action a;
foreach(Constrain c in Constrains)
{
if(!c.Allows(a))
{
a.Change();
restart;
}
}
重新启动
这里就像继续
或破
,但它从一开始$ b $重新启动的foreach b检查就好一个为
循环的计数器重新设定为0 ..
restart
here is like continue
or break
but it restarts the foreach from the beginingIt is like setting the counter of a for
loop to 0 again..
这有可能在C#?
编辑:我要感谢这两个迈赫达德Afshari和马赫什Velaga在我当前的实现让我发现了一个错误(指数= 0),这将不会被发现,否则..
I want to thank both Mehrdad Afshari and Mahesh Velaga for letting me discover a bug (index=0) in my current implementation, that would not have been discovered otherwise..
推荐答案
使用好老转到
:
restart:
foreach(Constrain c in Constrains)
{
if(!c.Allows(a))
{
a.Change();
goto restart;
}
}
如果你确诊为的gotophobia 100%时间由于某种原因(是的不的没有原因的一件好事),你可以尝试使用一个标志来代替:
If you're diagnosed with gotophobia 100% of the time for some reason (which is not a good thing without a reason), you can try using a flag instead:
bool restart;
do {
restart = false;
foreach(Constrain c in Constrains)
{
if(!c.Allows(a))
{
a.Change();
restart = true;
break;
}
}
} while (restart);
这篇关于重新启动在C#中的foreach循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!