Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 中断内部foreach循环并继续外部foreach循环_C#_.net_C# 4.0_.net 4.0 - Fatal编程技术网

C# 中断内部foreach循环并继续外部foreach循环

C# 中断内部foreach循环并继续外部foreach循环,c#,.net,c#-4.0,.net-4.0,C#,.net,C# 4.0,.net 4.0,如果我有一个嵌套的foreach循环,我如何中断内部循环并告诉外部循环在该点继续,而不在内部循环下面执行任何其他代码 foreach(var item in items) { foreach(var otheritem in otheritems) { if (!double.TryParse(otheritem)) { //break inner loop //continue outer loop so we never get to DoSt

如果我有一个嵌套的foreach循环,我如何中断内部循环并告诉外部循环在该点继续,而不在内部循环下面执行任何其他代码

foreach(var item in items)
{
  foreach(var otheritem in otheritems)
  {
    if (!double.TryParse(otheritem))
    {
      //break inner loop
      //continue outer loop so we never get to DoStuff()
    }
  }

  DoStuff();
}

你需要一个变量来控制它,就像你说的。。执行
中断操作

bool doStuff = true;
foreach(var item in items)
{
  doStuff = true;
  foreach(var otheritem in otheritems)
  {
    if (!double.TryParse(otheritem))
    {
      doStuff = false;
      break;
    }
  }

  if (doStuff)
       DoStuff();
}

用旗子怎么样

foreach(var item in items)
{
  bool flag = false;
  foreach(var otheritem in otheritems)
  {
    if (!double.TryParse(otheritem))
    {
        flag = true;
        break;
    }
  }
  if(flag) continue;

  DoStuff();
}
简单是最好的

  bool doStuff = true;
  foreach(var otheritem in otheritems)
  {
    if (!double.TryParse(otheritem))
    {
        doStuff = false;
        break;
    }
  }
  if(doStuff) DoStuff();
另一种方法是重构:

foreach(var outerItem in outerLoop) {
     Foo(outerItem);
}
...
void Foo(OuterItem item) {
    foreach(var innerItem in innerLoop) {
        if(someTest) return;
    }
    DoStuff();
}

返回
确保不会发生
DoStuff

Iirc中断;语句将只中断最近的循环,因此发出一个中断;在内部循环中,应继续使用外部循环中的下一项。

代码片段中不清楚,但如果您只需在
其他项中查找不可解析的值,则可以使用LINQ:

foreach(var item in items)
{
  bool shouldISkip = otheritems.Any(otherItem => !double.TryParse(otherItem));
  if(shouldISkip) continue;
  DoStuff();
}

首先编写Double.TryParse的更好版本:

static double? TryParseDouble(this string s)
{
    double d;
    return double.TryParse(s, out d) ? (double?)d : (double?)null;
}
好的,现在您可以轻松地使用一些工具来完全消除内部循环,因此问题消失了:

foreach(var item in items)
    if (!otheritems.Any(otherItem=>otherItem.TryParseDouble() == null))
        DoStuff();
与其试图找出如何移动控件,不如编写类似于逻辑的代码。如果逻辑是“如果任何其他项不作为double解析,则不执行任何操作”,则使用any谓词测试所有其他项,以查看是否有任何项不作为double解析。没有循环,因此不需要花哨的循环控制

我倾向于更进一步;捕获查询中的逻辑,然后迭代查询:

var goodItems = from item in items
                where !item.OtherItems.Any(otherItem=>otherItem.TryParseDouble() == null))
                select item;

foreach(var goodItem in goodItems)
    DoStuff(goodItem);

OP希望跳过外循环上的剩余代码并在外循环的顶部继续执行。我注意到这不是double.TryParse的签名。这可能是goto的唯一合法用法。不过我更喜欢java继续使用循环标签的方式。@BLUEPIXY我只是希望Anders能读到这篇文章,并在下一版本的C#中实现它。希望,男人可以。@BLUEPIXY-请停止对其他社区成员的光顾。这不像任何人对你的第一句评论说“没有幽默标签”。谢谢你们的合作,让这个社区变得更加友好。好的建议,尽管它没有回答问题:如何打破内部循环,同时继续外部循环?在本例中,您可以将代码简化为单个循环,但情况并非总是如此。
foreach(var item in items)
    if (!otheritems.Any(otherItem=>otherItem.TryParseDouble() == null))
        DoStuff();
var goodItems = from item in items
                where !item.OtherItems.Any(otherItem=>otherItem.TryParseDouble() == null))
                select item;

foreach(var goodItem in goodItems)
    DoStuff(goodItem);