加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C# – while循环中的foreach循环 – 打破foreach并立即继续while

发布时间:2020-12-15 18:19:20 所属栏目:百科 来源:网络整理
导读:while (foo() == true){ foreach (var x in xs) { if (bar(x) == true) { //"break;" out of this foreach //AND "continue;" on the while loop. } } //If I didn't continue,do other stuff.} 我有点坚持如何做到这一点. 更新:我修正了问题.我遗漏了一个
while (foo() == true)
{
   foreach (var x in xs)
   {
       if (bar(x) == true)
       {
           //"break;" out of this foreach
           //AND "continue;" on the while loop.
       }
   }

   //If I didn't continue,do other stuff.
}

我有点坚持如何做到这一点.

更新:我修正了问题.我遗漏了一个事实,即如果我不打电话给继续,我需要处理其他事情;在while循环中.

对不起,我没有意识到我曾两次使用“某事”这个词.

解决方法

如果我理解正确,您可以在此处使用LINQ Any/ All谓词:
while (something)
{
    // You can also write this with the Enumerable.All method
   if(!xs.Any(x => somePredicate(x))
   {
      // Place code meant for the "If I didn't continue,do other stuff."
      // block here.
   }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读