23

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.

10
  • 2
    To troubleshoot this, I would confirm what version of the compiler is being used by putting #error version in source (this will output the version of the compiler) and also use /langversion:? to list available versions. Jul 12, 2020 at 15:29
  • @JulienCouvreur I've tried what you suggested and it shows Compiler version: '2.9.0.63208 (958f2354)' the langversion shows exactly what I set via the project's Properties window. Thanks for your suggestion anyway however I have no clue about that info.
    – Hopeless
    Jul 12, 2020 at 16:04
  • Do you have the most recent version of the Microsoft.Net.Compilers Nuget package installed?
    – DMadden51
    Jul 12, 2020 at 23:07
  • @DMadden51 yes I do have that (already mentioned in my question), installed the latest version of Microsoft.CodeDom.Providers.DotNetCompilerPlatform as well.
    – Hopeless
    Jul 13, 2020 at 3:38
  • @Hopeless I am mystified :-/ Roslyn compiler version 2.9.x support language version 7.3 (documented here). It may be worth asking the ASP.NET team. There can be more than one compiler involved: IDE, Build and ASP.NET. Looking at a build log would help (use /binarylog option then "MSBuild structured log viewer"). If your project is small/simple/standalone and not confidential, you could share it with me to try and repro (email [email protected]). Jul 13, 2020 at 21:16

7 Answers 7

43

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.

1
  • 4
    2022 version: clean solution, close Visual Studio, reopen, rebuild solution. Sep 14, 2022 at 11:32
21

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

-Microsoft.CodeDom.Providers.DotNetCompilerPlatform

-Microsoft.Net.Compilers

1
  • 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.
    – dev'd
    Dec 15, 2021 at 15:37
12

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

1
  • It worked good to me in VS 2022 with .net framweork 4.7.2 Nov 10 at 16:35
3

Update the Microsoft.Net.Compilers nuget package.

1
1

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.

0

in case anyone encountering a similar error:

Project-->Properties-->Build-->Advanced-->then Change Language Version like this: [enter image description here

0

I had similar issue. Installing the .Net Framework 4.8 targeting package solved the problem.

  1. go to VS installer
  2. click Modify
  3. in Individual components, check .Net Framework 4.8 targeting package
  4. install
  5. open your solution and build it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.