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

11,540

Solution 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

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

-Microsoft.CodeDom.Providers.DotNetCompilerPlatform

-Microsoft.Net.Compilers

Solution 3

Update the Microsoft.Net.Compilers nuget package. It worked for me..cheers!

Share:
11,540
Hopeless
Author by

Hopeless

My dear dog has been living for more than 10 years, so cool... She died at the age of 14 on April 3rd 2020 :( Top 200 Vietnam's users

Updated on June 21, 2022

Comments

  • Hopeless
    Hopeless almost 2 years

    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.

  • AgentFire
    AgentFire over 2 years
    Didn't help me.
  • dev'd
    dev'd over 2 years
    This worked for me locally but it failed on the build server when I committed the changes with something to do with GetPathsOfAllDirectoriesAbove(). My build server wasn't originally reporting any problems, so before going down that rabbit hole, I found this solution: stackoverflow.com/a/64542445/1158234. I simply updated VS 2019 and the problem resolved itself for me locally. No changes required.
  • Ibrahim Hasan
    Ibrahim Hasan over 2 years
    This solved a problem lasted for 5 months. Thanks a million
  • Maciej Pszczolinski
    Maciej Pszczolinski about 2 years
    worked for me. upvoted!
  • Yunnosch
    Yunnosch about 2 years
    Please explain what additional insight your post provides on top of the answer stackoverflow.com/a/65643330/7733418 Or delete this please.