S1227 — break statements should not be used except for switch cases

Language
C#
Type
Code smell
Severity
Minor

This rule is deprecated, and will eventually be removed.

Why is this an issue?

break; is an unstructured control flow statement which makes code harder to read.

Ideally, every loop should have a single termination condition.

Noncompliant code example


int i = 0;
while (true)
{
  if (i == 10)
  {
    break;      // Non-Compliant
  }

  Console.WriteLine(i);
  i++;
}

Compliant solution


int i = 0;
while (i != 10) // Compliant
{
  Console.WriteLine(i);
  i++;
}

↑ Back to top