我是靠谱客的博主 怡然彩虹,最近开发中收集的这篇文章主要介绍如何跳过`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中,我可以跳过下一个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
}

#1楼

参考:https://stackoom.com/question/2kAD/如何跳过-foreach-循环的迭代


#2楼

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


#3楼

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

#4楼

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

#5楼

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: 更新:回应Brian在评论中的后续问题:

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始终适用于最近的封闭范围,因此您无法使用它来突破最外层循环。 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.
}
}

#6楼

Use the continue statement: 使用continue语句:

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

最后

以上就是怡然彩虹为你收集整理的如何跳过`foreach`循环的迭代?的全部内容,希望文章能够帮你解决如何跳过`foreach`循环的迭代?所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部