All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.csharp.rules.csharpsquid.SwitchWithoutDefault.html Maven / Gradle / Ivy

There is a newer version: 4.0
Show newest version

Always having a default: case in switch statements is defensive programming. It should either take appropriate action or contain a suitable comment as to why no action is taken.

The following code snippet:

int foo = 42;
switch (foo) // Non-Compliant
{
  case 0:
    Console.WriteLine("foo = 0");
    break;
  case 42:
    Console.WriteLine("foo = 42");
    break;
}

should be refactored into:

int foo = 42;
switch (foo) // Compliant
{
  case 0:
    Console.WriteLine("foo = 0");
    break;
  case 42:
    Console.WriteLine("foo = 42");
    break;
  default:
    throw new InvalidOperationException("Unexpected value foo = " + foo);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy