Roslyn: switch case without default no longer compiles in visual studio 2019 [c#]

Created on 16 Apr 2019  路  3Comments  路  Source: dotnet/roslyn

VSF_TYPE_MARKDOWNThe following code compiles fine in Visual Studio 2017, but not in Visual Studio 2019. Is there a default compiler option that has changed which can resolve this?

using System;

namespace NoDefaultCase
{
    public static class Program
    {
        public const string Database = "MongoDB";

        public static string GetDb()
        {
            switch (Database)
            {
                case "MongoDB":
                    return Database;
            }
        }

        public static void Main(string[] args)
        {
            Console.WriteLine($"The value returned is \"{GetDb()}\".");
            Console.WriteLine("Press a key to exit");
            Console.ReadKey();
        }
    }
}

There is also a Stack Overflow ticket for this item:
https://stackoverflow.com/questions/55541269/why-does-a-switch-case-statement-on-a-string-constant-require-a-default-in-visua

_This issue has been moved from https://developercommunity.visualstudio.com/content/problem/520522/switch-case-without-default-no-longer-compiles-in.html
VSTS ticketId: 841662_
_These are the original issue comments:_

Martin Eyles on 4/6/2019, 05:37 AM (10 days ago):

In Visual Studio 2017, the output of the build is:

1>------ Rebuild All started: Project: NoDefaultCase, Configuration: Debug Any CPU ------
1> NoDefaultCase -> C:\Users\MartinEyles\source\repos\NoDefaultCase\NoDefaultCase\bin\Debug\NoDefaultCase.exe
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

In Visual Studio 2019, the output of the build is:

1>------ Rebuild All started: Project: NoDefaultCase, Configuration: Debug Any CPU ------
1>C:\Users\MartinEyles\source\repos\NoDefaultCase\NoDefaultCase\Program.cs(9,30,9,35): error CS0161: 'Program.GetDb()': not all code paths return a value
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Visual Studio Feedback System on 4/8/2019, 02:55 AM (8 days ago):

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

Martin Eyles on 4/8/2019, 03:07 AM (8 days ago):

A github repository containing the example solution can be found at https://github.com/martineyles/NoDefaultCase This includes an archive of the example solution in the state before it was added to github (NoDefaultCase - Before Github.zip).

Martin Eyles on 4/8/2019, 05:59 AM (8 days ago):

I experience this issue in

Microsoft Visual Studio Enterprise 2019
Version 16.0.0
VisualStudio.16.Release/16.0.0+28729.10
Microsoft .NET Framework Version 4.7.03056'

It has been suggested by others in the Stack Overflow community that a preview version of Visual Studio 2019 behaves the same way as Visual Studio 2017

Martin Eyles on 4/12/2019, 05:24 AM (4 days ago):

The issue is still present in:

Microsoft Visual Studio Enterprise 2019
Version 16.0.1
VisualStudio.16.Release/16.0.1+28803.156
Microsoft .NET Framework Version 4.7.03056

Martin Eyles on 4/12/2019, 05:26 AM (4 days ago):

Stack overflow users has suggested that this does not meet the C# specification, and is likely to be either a bug or a change in the specification that hasn't yet been documented.

Martin Eyles on 4/12/2019, 05:29 AM (4 days ago):

To quote a user with very good reputation on stack overflow:

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鈥檛 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...

_These are the original issue solutions:_
(no solutions)

Area-Compilers Bug Developer Community

Most helpful comment

Is there any actual application for this?

All 3 comments

The description at the end is correct -- the switch is constant, so the end of the switch should not be reachable. This was an inadvertent break.

This has been fixed and should appear in VS updates as they come out.

Is there any actual application for this?

Was this page helpful?
0 / 5 - 0 ratings