HOWTO · Csharp
How to Exit a Foreach Loop in C#
Stop a C# foreach loop with break, or leave its containing method with return.
On this page
To stop a C# foreach loop when a condition is met, use break. It ends the nearest enclosing loop and continues with the first statement after that loop. Use return when the containing method should finish as well. For nested loops, carry the condition out of the inner loop with a flag, then break the outer loop.
Use break to Exit the Loop
This example searches a list for the value 4. The check comes before the print, so the matching value and later items are not processed by the loop. The example prints each value before 4, then continues after the loop.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };
foreach (int number in numbers)
{
if (number == 4)
{
break;
}
Console.WriteLine(number);
}
Console.WriteLine("Loop finished");
}
}
1
2
3
Loop finished
break exits only the nearest enclosing loop. Execution then reaches Console.WriteLine("Loop finished"), which is after the loop. In a foreach, iteration is over an enumerable sequence; the example uses a List<int>. Put the condition before work you want to skip for the matching item. If you put it after that work, the item will be processed before the loop stops.
return Exits the Containing Method
Choose return when reaching the condition means the method should stop doing all its remaining work. The example prints each value before 4, then returns to Main.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
PrintBeforeFour(new List<int> { 1, 2, 3, 4, 5, 6 });
Console.WriteLine("Back in Main");
}
static void PrintBeforeFour(List<int> numbers)
{
foreach (int number in numbers)
{
if (number == 4)
{
return;
}
Console.WriteLine(number);
}
Console.WriteLine("Method finished after the loop");
}
}
1
2
3
Back in Main
When return runs inside PrintBeforeFour, control goes back to its caller, so the method’s later statement is skipped. Main then continues after the call. This differs from break, which leaves the loop but continues within the same method. For a method that returns a value, each path must also provide a value of the declared return type; a void method can use a bare return.
Use a Flag for Nested Loop Exit Conditions
A break inside an inner loop exits that inner loop only. When a match found there should also stop an outer foreach, store that fact in a flag, then check the flag after the inner loop. The example prints values through 4, then stops both loops.
using System;
class Program
{
static void Main()
{
int[][] rows =
{
new[] { 1, 2 },
new[] { 3, 4 },
new[] { 5, 6 }
};
bool found = false;
foreach (int[] row in rows)
{
foreach (int value in row)
{
Console.WriteLine(value);
if (value == 4)
{
found = true;
break;
}
}
if (found)
{
break;
}
}
Console.WriteLine("Search finished");
}
}
1
2
3
4
Search finished
The inner break stops scanning the current row. The flag remains true after that loop, allowing the outer loop to stop at its next check. The value 4 is printed because this example prints each value before testing it; move the test first if the matching value should not be printed. A flag is useful when the decision arises in nested logic and the outer loop must respond. For a condition checked directly in the outer loop, a plain break is clearer.
Choose the Right Exit
Use break to end the current loop and continue the method. Use return to end the containing method too. Use a flag when an inner loop discovers that the outer loop must stop. If you only need to skip the rest of the current iteration and inspect the next item, use continue; it does not end the loop. These are C# control-flow statements, not method calls. Select the statement whose scope matches the work you want to stop, and place its condition before or after processing according to whether the matching item should be handled.