简体   繁体   English

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

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

In Perl I can skip a foreach (or any loop) iteration with a next;在 Perl 中,我可以使用 next 跳过 foreach(或任何循环)迭代next; command.命令。

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
 }

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 continue keyword .以下是关于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 循环并想跳过其中一个扩展循环的迭代,您能否进一步澄清我会做什么?

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

A continue always applies to the nearest enclosing scope, so you couldn't use it to break out of the outermost loop. continue始终适用于最近的封闭 scope,因此您不能使用它来跳出最外层的循环。 If a condition like that arises, you'd need to do something more complicated depending on exactly what you want, like break from the inner loop, then continue on the outer loop.如果出现这样的情况,你需要根据你想要的做一些更复杂的事情,比如从内部循环中break ,然后在外部循环上continue See here for the documentation on the break keyword .有关break关键字的文档,请参见此处。 The break C# keyword is similar to the Perl last keyword. break C# 关键字类似于 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.
  }
}

Another approach is to filter using LINQ before the loop executes:另一种方法是在循环执行之前使用 LINQ 进行过滤:

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

You could also flip your if test:你也可以翻转你的 if 测试:


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

    //otherwise process number
}

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 .SkipWhere if you want to specify a condition for skipping.或者,如果要指定跳过条件,请使用.SkipWhere

You can use the continue statement.您可以使用continue语句。

For example:例如:

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

Use the continue statement:使用 continue 语句:

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

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM