7

Using an interpolated string in a .cshtml view is giving the following intellisense error: Feature 'interpolated strings' is not used in C#5. Please use language version 6 or greater. This and other C#7 language features are working in compiled code (.cs files).

As you can see below, the latest major version is C#7.

enter image description here

According to a comment in this question, "default" means "latest major version".

So why the error? Also, why doesn't it show "Latest Major Version" and "Latest Minor Version" as separate list options, as I have seen in many online examples?

UPDATE:

I finally managed to get string interpolation working in my views by installing the CodeDom providers package (The Microsoft.Net.Compilers package is related to Msbuild. The CodeDOM Providers package is related to ASP.NET and other apis that compile at runtime hence why cshtml files will show errors if this is not installed even if the latest language version is selected for msbuild). This automatically added the following to my web.config:

 <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>

Ref: C# 6.0 Features Not Working with Visual Studio 2015

Upgrading to MVC6 would also have fixed it I believe.

Kev
  • 2,656
  • 3
  • 39
  • 63
  • 3
    Which version of VS2017 are you using? Your screenshot looks reminiscent of the RC versions; not the released versions. – David Arno Oct 05 '17 at 14:37
  • @DavidArno - Enterprise. – Kev Oct 05 '17 at 14:38
  • Sorry, I meant version as in eg 15.3.5. You can find that out via `Help -> About Microsoft Visual Studio` – David Arno Oct 05 '17 at 14:45
  • @DavidArno interesting - it says 15.0.0+26228.10. There is an update available - should I download it or do I need to reinstall. I thought I installed quite recently - although maybe my memory is wrong. – Kev Oct 05 '17 at 14:48
  • 3
    There have been a great many fixes to Visual Studio applied through numerous updates since it's release. So I'd recommend downloading that update as it more than likely will address your problem. – David Arno Oct 05 '17 at 14:50
  • I updated and now get "Latest Major Version" etc in the list. Still haven't solved my problem though. – Kev Oct 05 '17 at 20:43
  • I'm with @JulienCouvreur in thinking that it's likely a web config issue for you therefore. – David Arno Oct 06 '17 at 08:17

1 Answers1

4

I suspect the key to your question is when you say "in a view". Assuming this is in the context of ASP.Net, you should look at your web.config, which may specify it's own LangVersion setting (likely hardcoded to 5 in your case).

More details on configuring ASP.Net to use newer versions of C# can be found in this Roslyn documentation issue.

Julien Couvreur
  • 4,473
  • 1
  • 30
  • 40
  • No LangVerson in my web.config. I tried installing Microsoft.Net.Compilers (even though I shouldn't have to in VS2017) as per the suggestion in the linked page. Still no LangVersion. Still giving errors. – Kev Oct 05 '17 at 20:40
  • Can you share your web.config? There should be something like: `` – Julien Couvreur Oct 06 '17 at 02:22
  • No there is nothing like that. I can use string interpolation in .cs files, but not .cshtml files. – Kev Oct 06 '17 at 08:20
  • 1
    Got it working by installing the codedom providers (The Microsoft.Net.Compilers is related to Msbuild. the CodeDOM Providers is related to ASP.NET and other api's that compile at runtime) – Kev Oct 06 '17 at 09:12