0

I am developing a website using the latest version of VS2019 (16.4.5) and I am targeting the .NET Framework 4.7.2.

However, I have this kind of code in my Razor views

@(Model.Data?.Title)

which keeps giving me

Feature 'null propagating operator' is not available in C# 4. Please use language version 6 or greater

I thought that by using VS2019 and targeting .NET Framework 4.7.2, I would be at least using C#6. In this version of VS, you can not even have access to the version of C# through Project Properties > Build > Advanced > Language version as it is now greyed. Instead, I have "Automatically selected based on framework version"...

I have also checked my compiler and I have Microsoft.CodeDom.Providers.DotNetCompilerPlatform 2.0.1 Nuget package so it sounds fine to me.

Based on this article, I should be at least in 7.3 for the C# version so why do I keep getting this error?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Sylvain C.
  • 1,043
  • 1
  • 11
  • 27
  • Does your csproj have `4` in it? – Jonathon Chase Feb 21 '20 at 23:13
  • No, I have checked my csproj and the LangVersion tag is not even used – Sylvain C. Feb 21 '20 at 23:14
  • 1
    In web.config, check the `/langversion` value in the `` tag under ``. – madreflection Feb 21 '20 at 23:15
  • For c#, I have compilerOptions="/langversion:default /nowarn:1659;1699;1701" so the version should be driven by the .Net Framework, no? – Sylvain C. Feb 21 '20 at 23:18
  • Try `/langversion:latest`. Not sure why 'default' doesn't work, though. Maybe the roslyn folder isn't being copied correctly, overwriting the older version. – madreflection Feb 21 '20 at 23:18
  • Rebuilt everything, reopened VS2019 but still the same error. Is there a way to know which version of C# I am targeting? – Sylvain C. Feb 21 '20 at 23:23
  • You could specify the version instead, `/langversion:6` or `/langversion:7.3`. Doing that should cause a view compilation error if "default" and "latest" were not as high as we were expecting. Frankly, this was always difficult for me to diagnose the few times I had to deal with compilation errors related to the Roslyn CodeDOM providers. Lots of narrowly reading error messages followed by trial and error. – madreflection Feb 21 '20 at 23:24
  • Thanks @HimBromBeere, I had already read this one and this is why I am surprised to have such an error dealing with VS2019 and the default compiler. By the way, this error appears only when I try to publish to my production server with "Precompile during publishing" checked. If I unchecked it, the Publishing works and I have no error. – Sylvain C. Feb 21 '20 at 23:34
  • Followed some recommendation from https://stackoverflow.com/questions/32282880/publish-website-without-roslyn?rq=1 and I rechecked the "Precompile during publishing" but also the "Allow precompiled site to be updatable" and I was able to publish without error even if I still have the C#4.0 error. Would you recommend to uninstall the DotNetCompilerPlatform package? – Sylvain C. Feb 21 '20 at 23:56
  • You definitely don't want to remove that package. – madreflection Feb 22 '20 at 00:03
  • The errors only appears for views available in \obj\Release\AspnetCompileMerge\Source\Views\ after trying to publish and compile the views. The same views in my solution tree structure are fine and do not show errors. What could cause this difference? – Sylvain C. Feb 24 '20 at 21:28

0 Answers0