32

There is a similar question to this here but I believe that involves a different cause.

I moved a class from a newer project into an older project. Both were targeting .net 4.6 however after the move I received the following error on build.

Feature 'interpolated strings' is not available in C# 5. Please use language version 6 or greater.

I tried setting my project to build with C# 6 in the properties window with no change.

Community
  • 1
  • 1
TheColonel26
  • 2,618
  • 7
  • 25
  • 50
  • Makes sense since interpolated strings were added in 6.0 and you tried to run them against 5.0. Ref: https://msdn.microsoft.com/en-us/library/dn961160.aspx?f=255&MSPPError=-2147217396 – Tdorno Feb 28 '16 at 19:25
  • @Tdorno: yes but usually C#6 is the default language version for a .net 4.6 project. In this case it was not. – TheColonel26 Feb 28 '16 at 20:22
  • @TheColonel26 The default language for _any_ project is "default." For it to be set to C# 5, it must have been changed explicitly at some point. Language version is in no way related to the version of .NET you target. – BJ Myers Feb 29 '16 at 01:53
  • @BJ Myers that is good to know. I am using VS 2015 though. The project was originally created in 2012 however which now hind sight being 20/20 it makes sense that it would still be set to version 5. – TheColonel26 Feb 29 '16 at 01:55
  • For me, this answer resolve the problem https://stackoverflow.com/a/36575516/6640473 – Richard Lindner Oct 27 '17 at 12:06

5 Answers5

23

I eventually found the place to change it. It seems sometimes when you update your targets framework version this does not get changed. enter image description here

TheColonel26
  • 2,618
  • 7
  • 25
  • 50
  • 3
    Changing the target framework should no change the language version and changing the language version should no change the target framework. – Paulo Morgado Feb 28 '16 at 20:55
  • I have visual studio community update 3 and cannot get to this screen. The closest thing I've found comes from the property pages, (it has a "build" option below "References" not "Application" and there is no "Advanced" button. – Malcolm Anderson Mar 19 '17 at 16:17
  • @MalcolmAnderson What type of project is this? – TheColonel26 Apr 04 '17 at 17:05
  • @TheColonel26 - I *believe* that it was a web forms project, but honestly, I don't remember where I had the issue in order to confirm. If I remember correctly, this was fixed for me by adding some lines to my webconfig. – Malcolm Anderson Apr 10 '17 at 19:47
  • I applied this change, but I'm still getting the error. What gives? – Dan Csharpster Aug 15 '17 at 21:03
  • @DanCsharpster Can you elaborate? what .Net Framework version are you using. What Visual Studio version are you using? – TheColonel26 Aug 15 '17 at 23:02
  • Are use using .net 4.5.2 or later? – TheColonel26 Aug 15 '17 at 23:03
  • .Net 4.6.1. I switched version of C# to 6 and also tried 7 to no avail. I unloaded and reloaded the project and also tried restarting visual studio and it still whines with the same error that interpolated strings is not available in C# 5. This is an ASP.NET web project as part of a larger solution. Do I need to check all of the projects that mine references as well? – Dan Csharpster Aug 16 '17 at 02:26
  • I would try doing a rebuild on your lowest dependency and work your way up until something doesn't build. I've had some weird issues with change .net target versions and a solution rebuild did not work. I had to rebuild each project individual all the way up the dependency stack and then they all worked. – TheColonel26 Aug 17 '17 at 00:01
  • 2
    The answer is here https://stackoverflow.com/questions/31548699/how-to-use-c-sharp-6-with-web-site-project-type, you need to add a NuGet Package to make it work. – chrilith Sep 04 '17 at 10:26
  • 1
    Don't forget to set it on both Debug and Release builds (or "All Configurations") – Martin Randall Sep 13 '17 at 18:40
22

Install DotNetCompilerPlatform version 2.1.0

Navid
  • 609
  • 6
  • 6
  • 3
    i have a website not webapplication, this is the working solution for that. Found it in another article that we could do that by selecting the website then from the menu, website->Enable c#6 /VB 14 adds these packages as well – Esen Feb 27 '18 at 15:35
  • 1
    Latest stable (3.6.0 as of this writing) also seems to work. – derekbaker783 Jan 19 '21 at 19:37
8

(It can applicable VS 2019 - .NET Framework 4.8 Web Application projects easily)

I have realized this issue after install DotNetCompilerPlatform v3.6

I have looked for TheColonel26's answer but I couldn't change selected language version:

Advanced Build Setting - Language Version Selection

Appearantly, we can not change selected language version. (For details look here)

After that I have used kfwbird's answer but with changes for newer version:

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

Now it works as should be.

bafsar
  • 1,080
  • 2
  • 16
  • 16
  • 1
    I did a de-install and re-install of this nuget package; then had to close the views and re-open them; Closing VS would have probably done the same thing. Manually adding to the Web.config did not work. Now on to the other 250+ projects sigh.... – Mark Schultheiss Nov 22 '22 at 17:42
  • @MarkSchultheiss try to clean the project first. Maybe it helps. Also be sure you are using the same version of the package. Now there are newer version of it. It is possible that you are installing wrong version. – bafsar Nov 23 '22 at 12:50
5

Add this to your web.config. It is probably added automatically after installing DotNetCompilerPlatform.

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

The GUI would not let me change the version, but I could change manually in the csproj-file.

<LangVersion>5</LangVersion>
to
<LangVersion>6</LangVersion>

leiflundgren
  • 2,876
  • 7
  • 35
  • 53