简体   繁体   English

如何退出 C# 中的 foreach 循环?

[英]How do I exit a foreach loop in C#?

foreach (var name in parent.names)
{
    if name.lastname == null)
    {
        Violated = true;
        this.message = "lastname reqd";
    }

    if (!Violated)
    {
        Violated = !(name.firstname == null) ? false : true;
        if (ruleViolated)
            this.message = "firstname reqd";
    }
}

Whenever violated is true, I want to get out of the foreach loop immediately.每当违反为真时,我想立即退出foreach循环。 How do I do it?我该怎么做?

Use break .使用break


Unrelated to your question, I see in your code the line:与您的问题无关,我在您的代码中看到了这一行:

Violated = !(name.firstname == null) ? false : true;

In this line, you take a boolean value (name.firstname == null) .在这一行中,您使用一个布尔值(name.firstname == null) Then, you apply the !然后,您应用! operator to it.运营商给它。 Then, if the value is true, you set Violated to false;然后,如果值为 true,则将 Violated 设置为 false; otherwise to true.否则为真。 So basically, Violated is set to the same value as the original expression (name.firstname == null) .所以基本上, Violated 被设置为与原始表达式(name.firstname == null)相同的值。 Why not use that, as in:为什么不使用它,如:

Violated = (name.firstname == null);

只需使用以下语句:

break;

使用break关键字。

Look at this code, it can help you to get out of the loop fast!看看这段代码,它可以帮助你快速跳出循环!

foreach (var name in parent.names)
{
   if (name.lastname == null)
   {
      Violated = true;
      this.message = "lastname reqd";
      break;
   }
   else if (name.firstname == null)
   {
      Violated = true;
      this.message = "firstname reqd";
      break;
   }
}

During testing I found that foreach loop after break go to the loop beging and not out of the loop.在测试期间,我发现 break 之后的 foreach 循环进入循环开始而不是循环之外。 So I changed foreach into for and break in this case work correctly- after break program flow goes out of the loop.因此,我将 foreach 更改为 for 并在这种情况下 break 正常工作 - 在 break 程序流程退出循环之后。

break; - to exit foreach - 退出 foreach

continue; - jump to next value - 跳转到下一个值

Instead of foreach loop而不是foreach循环

foreach (var name in parent.names)
{
    if name.lastname == null)
    {
        Violated = true;
        this.message = "lastname reqd";
    }

    if (!Violated)
    {
        Violated = !(name.firstname == null) ? false : true;
        if (ruleViolated)
            this.message = "firstname reqd";
    }
}

make use of equivalent for loop使用等效for循环

bool Violated = false;

for (int i = 0; i < parent.names.Count && !Violated; i++)
{
    var name = parent.names[i];
    
    if name.lastname == null)
    {
        Violated = true;
        this.message = "lastname reqd";
    }

    if (!Violated)
    {
        Violated = !(name.firstname == null) ? false : true;
        if (ruleViolated)
            this.message = "firstname reqd";
    }
}

With the condition i < parent.names.Count && !Violated you get full control to stop the loop safe without any break or return or whatever to brutally force the foreach to stop.在条件i < parent.names.Count && !Violated的情况下,您可以完全控制安全地停止循环,而不会出现任何breakreturn或任何粗暴地强制foreach停止的情况。

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

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