Why does a switch-case statement on a string constant require a default in Visual Studio 2019 (prior to 16.0.3) but not in Visual Studio 2017?
It looks like either the specification will be updated with new rules on reachability, or this is a bug in Roslyn, probably due to changes introduced with switch expressions.
The important question for the compiler is whether the end of the method is reachable - which it will be if and only if the end of the switch statement is reachable.
The ECMA C# 5 standard section 13.8.3 describes the reachability of the end of a switch statement:
The end point of a switch statement is reachable if at least one of the following is true:
- The switch statement contains a reachable break statement that exits the switch statement.
- The switch statement is reachable, the switch expression is a non-constant value, and no default label is present.
- The switch statement is reachable, the switch expression is a constant value that doesn’t match any case label, and no default label is present.
None of these seem to be the case in your example:
- There are no break statements
- The switch expression is a constant value
- The constant value does match a case label
So with C# 5 rules, the end point of this switch statement is not reachable, and it should compile with no problems. The draft specification in GitHub has the same text, so it doesn't look like it's changed there yet...