C# 如何跳过“foreach”循环的迭代?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/654113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 12:00:18  来源:igfitidea点击:

How do I skip an iteration of a `foreach` loop?

c#.netloops

提问by Brian

In Perl I can skip a foreach (or any loop) iteration with a next;command.

在 Perl 中,我可以使用next;命令跳过 foreach(或任何循环)迭代。

Is there a way to skip over an iteration and jump to the next loop in C#?

有没有办法跳过迭代并跳转到 C# 中的下一个循环?

 foreach (int number in numbers)
 {
     if (number < 0)
     {
         // What goes here to skip over the loop?
     }

     // otherwise process number
 }

采纳答案by John Feminella

You want:

你要:

foreach (int number in numbers) //   <--- go back to here --------+
{                               //                                |
    if (number < 0)             //                                |
    {                           //                                |
        continue;   // Skip the remainder of this iteration. -----+
    }

    // do work
}

Here's more about the continuekeyword.

这里有更多关于continue关键字的信息



Update:In response to Brian's follow-up question in the comments:

更新:回应布莱恩在评论中的后续问题:

Could you further clarify what I would do if I had nested for loops, and wanted to skip the iteration of one of the extended ones?

for (int[] numbers in numberarrays) {
  for (int number in numbers) { // What to do if I want to
                                // jump the (numbers/numberarrays)?
  }
}

如果我嵌套了 for 循环,并想跳过其中一个扩展循环的迭代,您能否进一步说明我会怎么做?

for (int[] numbers in numberarrays) {
  for (int number in numbers) { // What to do if I want to
                                // jump the (numbers/numberarrays)?
  }
}

A continuealways applies to the nearest enclosing scope, so you couldn't use it to break out of the outermost loop. If a condition like that arises, you'd need to do something more complicated depending on exactly what you want, like breakfrom the inner loop, then continueon the outer loop. See here for the documentation on the breakkeyword. The breakC# keyword is similar to the Perl lastkeyword.

Acontinue总是适用于最近的封闭范围,所以你不能用它来跳出最外层的循环。如果出现这样的情况,您需要根据您的需求做一些更复杂的事情,比如break从内循环开始,然后continue在外循环上。有关break关键字的文档,请参见此处。在breakC#的关键字是类似于Perl的last关键字。

Also, consider taking Dustin's suggestion to just filter out values you don't want to process beforehand:

另外,考虑采纳 Dustin 的建议,只过滤掉您不想事先处理的值:

foreach (var basket in baskets.Where(b => b.IsOpen())) {
  foreach (var fruit in basket.Where(f => f.IsTasty())) {
    cuteAnimal.Eat(fruit); // Om nom nom. You don't need to break/continue
                           // since all the fruits that reach this point are
                           // in available baskets and tasty.
  }
}

回答by Tamas Czinege

foreach ( int number in numbers )
{
    if ( number < 0 )
    {
        continue;
    }

    //otherwise process number
}

回答by drewh

Use the continue statement:

使用 continue 语句:

foreach(object number in mycollection) {
     if( number < 0 ) {
         continue;
     }
  }

回答by Kev

You can use the continuestatement.

您可以使用该continue语句。

For example:

例如:

foreach(int number in numbers)
{
    if(number < 0)
    {
        continue;
    }
}

回答by crashmstr

You could also flip your if test:

你也可以翻转你的 if 测试:


foreach ( int number in numbers )
{
     if ( number >= 0 )
     {
        //process number
     }
 }

回答by Dustin Campbell

Another approach is to filter using LINQ before the loop executes:

另一种方法是在循环执行之前使用 LINQ 进行过滤:

foreach ( int number in numbers.Where(n => n >= 0) )
{
    // process number
}

回答by Edmund Covington

Another approach using linq is:

使用 linq 的另一种方法是:

foreach ( int number in numbers.Skip(1))
{   
    // process number  
}

If you want to skip the first in a number of items.

如果要跳过多个项目中的第一个。

Or use .SkipWhereif you want to specify a condition for skipping.

或者.SkipWhere如果要指定跳过条件,请使用。

回答by Kashif

The easiest way to do that is like below:

最简单的方法如下:

//Skip First Iteration

foreach ( int number in numbers.Skip(1))

//Skip any other like 5th iteration

foreach ( int number in numbers.Skip(5))