重新启动在C#中的foreach循环? [英] Restart a foreach loop in C#?

查看:156
本文介绍了重新启动在C#中的foreach循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能重新启动的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 begining It 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循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆