122
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. How do I do it?

7 Answers 7

245

Use 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). Then, you apply the ! operator to it. Then, if the value is true, you set Violated to false; otherwise to true. So basically, Violated is set to the same value as the original expression (name.firstname == null). Why not use that, as in:

Violated = (name.firstname == null);
1
  • 2
    I don't like to see needless if tests and negation. Oct 16, 2019 at 21:15
163

Just use the statement:

break;
0
42

Use the break keyword.

20

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;
   }
}
10

break; - to exit foreach

continue; - jump to next value

0

Instead of foreach loop

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

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;
        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.

Sidenotes

  • I changed Violated = !(name.firstname == null) ? false : true; to equivalent expression Violated = name.firstname == null;.
  • I guess ruleViolated is different to Violated.
2
  • Unnecessarily making the code complex. Sep 30, 2022 at 7:28
  • @AbhishekDutt: Hello, where is the complexity for you? Is it because of using for instead foreach? Or not using break?
    – Daniel
    Feb 1 at 8:21
-1

During testing I found that foreach loop after break go to the loop beging and not out of the loop. So I changed foreach into for and break in this case work correctly- after break program flow goes out of the loop.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.