C# 在 foreach 中继续

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/621266/
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 10:37:00  来源:igfitidea点击:

Continue in while inside foreach

c#foreachkeywordwhile-loopcontinue

提问by Amr

In the following C# code snippet
I have a 'while' loop inside a 'foreach' loop and I wish to jump to the next item in 'foreach' when a certain condition occurs.

在下面的 C# 代码片段中,
我在 ' while' 循环中有一个 ' foreach' 循环,我希望在foreach特定条件发生时跳转到 ' ' 中的下一项。

foreach (string objectName in this.ObjectNames)
{
    // Line to jump to when this.MoveToNextObject is true.
    this.ExecuteSomeCode();
    while (this.boolValue)
    {
        // 'continue' would jump to here.
        this.ExecuteSomeMoreCode();
        if (this.MoveToNextObject())
        {
            // What should go here to jump to next object.
        }
        this.ExecuteEvenMoreCode();
        this.boolValue = this.ResumeWhileLoop();
    }
    this.ExecuteSomeOtherCode();
}

'continue' would jump to the beginning of the 'while' loop not the 'foreach' loop. Is there's a keyword to use here, or should I just use goto which I don't really like.

' continue' 会跳到 ' while' 循环的开头而不是 ' foreach' 循环。这里是否有关键字可以使用,或者我应该只使用我不太喜欢的 goto。

采纳答案by Henk Holterman

The following should do the trick

以下应该可以解决问题

foreach (string objectName in this.ObjectNames)
{
    // Line to jump to when this.MoveToNextObject is true.
    this.ExecuteSomeCode();
    while (this.boolValue)
    {
        if (this.MoveToNextObject())
        {
            // What should go here to jump to next object.
            break;
        }
    }
    if (! this.boolValue) continue; // continue foreach

    this.ExecuteSomeOtherCode();
}

回答by Chris Hynes

Use the break keyword. That will exit the while loop and continue execution outside it. Since you don't have anything after the while, it would loop around to the next item in the foreach loop.

使用 break 关键字。这将退出 while 循环并在它之外继续执行。由于在 while 之后您没有任何东西,它会循环到 foreach 循环中的下一项。

Actually, looking at your example more closely, you actually want to be able to advance the for loop without exiting the while. You can't do this with a foreach loop, but you can break down a foreach loop to what it actually automates. In .NET, a foreach loop is actually rendered as a .GetEnumerator() call on the IEnumerable object (which your this.ObjectNames object is).

实际上,更仔细地查看您的示例,您实际上希望能够在不退出 while 的情况下推进 for 循环。您不能使用 foreach 循环来做到这一点,但您可以将 foreach 循环分解为它实际自动化的内容。在 .NET 中,foreach 循环实际上呈现为对 IEnumerable 对象(您的 this.ObjectNames 对象)的 .GetEnumerator() 调用。

The foreach loop is basically this:

foreach 循环基本上是这样的:

IEnumerator enumerator = this.ObjectNames.GetEnumerator();

while (enumerator.MoveNext())
{
    string objectName = (string)enumerator.Value;

    // your code inside the foreach loop would be here
}

Once you have this structure, you can call enumerator.MoveNext() within your while loop to advance to the next element. So your code would become:

一旦你有了这个结构,你就可以在你的 while 循环中调用 enumerator.MoveNext() 以前进到下一个元素。所以你的代码会变成:

IEnumerator enumerator = this.ObjectNames.GetEnumerator();

while (enumerator.MoveNext())
{
    while (this.ResumeWhileLoop())
    {
        if (this.MoveToNextObject())
        {
            // advance the loop
            if (!enumerator.MoveNext())
                // if false, there are no more items, so exit
                return;
        }

        // do your stuff
    }
}

回答by Mitch Wheat

The break;keyword will exit a loop:

break;关键字将退出循环:

foreach (string objectName in this.ObjectNames)
{
    // Line to jump to when this.MoveToNextObject is true.
    while (this.boolValue)
    {
        // 'continue' would jump to here.
        if (this.MoveToNextObject())
        {
            break;
        }
        this.boolValue = this.ResumeWhileLoop();
    }
}

回答by andleer

You can use "break;" to exit the innermost while or foreach.

你可以使用“break;” 退出最里面的 while 或 foreach。

回答by erikkallen

Use goto.

使用goto.

(I guess people will be mad with this response, but I definitely think it's more readable than all other options.)

(我想人们会对这个回应感到生气,但我绝对认为它比所有其他选项更具可读性。)