'Invalid option '7.3' for /langversion; must be ISO-1, ISO-2, Default or an integer in range 1 to 6

I'm using Visual Studio 17 (version 15.8.5), my project targets .NET Framework 4.8 and I've tried setting the C# version to use (via Build tab in the Properties window) C# 7.3 (that's the maximum version selectable from the dropdown list). However building the project reports a failure like what in the question title. I've even tried 7.0 and C# latest minor version, all those report failure as well.

Looks like the environment supports the maximum version of 6 (C# 6.0)?

I've tried installed both Microsoft.Net.Compilers v3.6.0 and Microsoft.Net.Compilers.Toolset v3.6.0 but it changed nothing.

I would like to use the pattern matching feature as the following simple code does:

object o ...
if(o is int c){
    //do something with c (cast to int) ...
}

UPDATE:

It's strange that if I create a completely new ASP.NET MVC project , it just compiles OK with C# 7.3 set. So looks like there is something wrong with just the specific project I'm working on. For better diagnosing, I would like to share that project here via this link https://sites.google.com/site/ngaodaantoi/files/WebApiAuth.zip?attredirects=0&d=1

It's recommended that you have VS 2017 to help possibly reproduce the issue.



Solution 1:[1]

I resolved this issue wihout upgrading any NuGet packages by following these steps:

  1. Close all open instances of Visual Studio

  2. Open "Visual Studio Installer"

  3. Click "Modify" on the version of Visual Studio you're using

    i. In my case, I'm using VS2019 Professional 16.11.0

  4. At the top of the dialog, select the "Individual Components"

  5. In the search bar, type "Roslyn"

  6. Check both options, should look like this

    i. In case the image goes down, the options are ".NET Compiler Platform SDK" and "C# and Visual Basic Roslyn Compilers"

    ii. In my case, the second one was already checked

  7. Confirm changes by pressing "Modify" at the bottom right of the dialog

Once I reopened my project, I was able to compile successfully.

Solution 2:[2]

Try upgrading below NuGet packages (whichever is installed) to resolve the problem:

-Microsoft.CodeDom.Providers.DotNetCompilerPlatform

-Microsoft.Net.Compilers

Solution 3:[3]

Update the Microsoft.Net.Compilers nuget package.

Solution 4:[4]

All of the projects created in VS, there is a property under Build tab named "Platform target" which has some dedicated value like x64, any and x86. When you open instance of VS for a particular solution, default in top menubar is "any cpu" which makes a conflict. Make sure you have common config everywhere, this will give you a good build.

Solution 5:[5]

For us, a simple clean & rebuild resolved the issue, without changing any other settings or configurations.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 ketan27j
Solution 3 Yunnosch
Solution 4 Amardeep Kumar Agrawal
Solution 5 Zimano