139

I am testing Visual Studio 2015 with C# 6.0 but the language features are not working. In an MVC web application, the following code does compile:

if (!string.IsNullOrWhiteSpace(Model.Profile?.TypeName))
{
    // More logic here...
}

However, when I run the application via Debug and IIS Express, I get the following error:

CS1525: Invalid expression term '.'

How do I enable these features?

jbtule
  • 31,383
  • 12
  • 95
  • 128
Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
  • 1
    This sounds like it's MVC-specific (or at least ASP.NET-specific) so I've added a tag - because Visual Studio itself clearly knows about C# 6... you should look at wherever you configure MVC in terms of the version of .NET it's targeting... it's possible that there's something there which will let you specify the compiler to use. – Jon Skeet Jan 15 '15 at 17:11
  • Maybe you need to add a `compiler` element for the roslyn codeprovider http://msdn.microsoft.com/en-us/library/y9x69bzw(v=vs.110).aspx in the web.config? – rene Jan 15 '15 at 17:15
  • Is this code that's in an aspx page or Razor page? or code that's in a .cs file? – Erik Funkenbusch Jan 15 '15 at 17:19
  • @ErikFunkenbusch It's in a Razor page. – Chris Schiffhauer Jan 15 '15 at 17:19
  • Are you using MVC5 or MVC6? – Erik Funkenbusch Jan 15 '15 at 17:20
  • I'm using MVC5. Looks like I need to get the project ported to MVC6... – Chris Schiffhauer Jan 15 '15 at 17:21
  • 1
    Yeah, MVC5 isn't Roslyn compatible – Erik Funkenbusch Jan 15 '15 at 17:24
  • I had this issue with one single Razor view only. This was apparently caused by the fact that one project was referencing `MVC 5.2.6` via NuGet and the other was a **manual reference** added to `MVC 5.2.3` This conflict didn't show up on the NuGet Manage Solution > Consolidate tab because it was a **manual reference**. Once I updated it to be `MVC 5.2.6` this error went away. – Shelby115 Jun 25 '18 at 13:50

7 Answers7

215

This works in MVC 5 (tested 5.2.3), you just need to add the roslyn code dom Nuget package

CodeDOM Providers for .NET Compiler...

Replacement CodeDOM providers that use the new .NET Compiler Platform ("Roslyn") compiler as a service APIs. This provides support for new language features in systems using CodeDOM (e.g. ASP.NET runtime compilation) as well as improving the compilation performance of these systems.

PM> Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform

https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/

jbtule
  • 31,383
  • 12
  • 95
  • 128
  • 4
    This should be submitted as an answer, because it fixes the problem rather than having to upgrade to a beta version of MVC. – Daniel Eugen Jul 25 '15 at 02:19
  • 3
    @Deef you are incorrect. The *Microsoft.Net.Compilers* is related to Msbuild. the *CodeDOM Providers* is related to ASP.NET and other api's that compile at runtime. – jbtule Aug 14 '15 at 13:11
  • 1
    Yeah, you're right. I misread/misinterpretted because they sometimes repalces packages with new names and just add them as dependencies. – David De Sloovere Aug 14 '15 at 16:57
  • 23
    It might be worth mentioning to restart Visual Studio. It kept complaining after I installed the package, but a restart made it go away :) – Siewers Sep 25 '15 at 13:50
  • When I run that I get a message saying "Install-Package : Unable to find package 'Microsoft.CodeDom.Providers.DotNetCompilerPlatform'" How do I get around this? – Rono Oct 30 '15 at 15:43
  • 1
    @Rono, Make sure your package source is `nuget.org`. As you can see from the link, it is the correct name, and the package exists. – jbtule Oct 30 '15 at 15:49
  • Had to delete all the dlls in the bin folder first before it would build – arame3333 Dec 15 '16 at 08:26
  • 1
    This answer also works for VS2017 RC4. In addition if you are using ReSharper you may have to restart VS to make the red squiggly lines go away. I'm using VS2017 RC4 with R# 2016.3.2. – Manfred Feb 22 '17 at 20:56
  • This answer worked for me: VS2017 v15.7.3. I got the error message "Error CS8026 Feature 'interpolated strings' is not available in C# 5. Please use language version 6 or greater." when using c# > v5 featrues in razor views in a asp.net mvc 4.6.1 project. See also (http://dave.ninja/c-6/using-c-6-0-features-in-asp-net-mvc-razor/ and https://dusted.codes/using-csharp-6-features-in-aspdotnet-mvc-5-razor-views) – mortenma71 Jun 24 '18 at 10:21
17

Well, I have MVC5 and recently installed VS 2015.

I've installed CodeDOM providers package, but it didn't help... But after that I realized, that package supports framework 4.5 only, while I have target framework set to 4.6 during tests - it works with 4.5 though...

So pay attention also to target framework. If you have 4.5 - just install package Microsoft.CodeDom.Providers.DotNetCompilerPlatform. But if you have 4.5.1-4.6 as a target, you will have to change in web.config section

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

For C#, just change type to:

type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Sergey Kljopov
  • 319
  • 2
  • 7
  • This certainly helped me getting `C# 6` to work with `ASP.NET 4.6`, but it might have been better to post the code with the correct `type` already there... – Serj Sagan Sep 11 '15 at 20:43
  • Thanks, this had been working when I went home on a Tuesday and then didn't work when I came back on the Thursday. Somehow, this section of the config had gone AWOL, because I remember seeing it when I was getting it to work originally! – Carl Dec 01 '16 at 10:52
16

I was having this same problem in Visual Studio 2015. Another answer here alluded to the solution I used, but they incorrectly specified the fix and never gave clarification.

On the the Visual Studio menu, select Project and you should see the sub-item Enable C#6 / VB 14 if you are having this problem. Select this menu sub-item. It will download the correct packages from Nuget and install them. After this, restart Visual Studio and reload your solution.

I cannot verify if this will also fix the Project Properties > Build > Advanced > Language Version selection to C# 6, so you might want to check this as well after enabling C# 6 from the menu.

KreepN
  • 8,528
  • 1
  • 40
  • 58
Tom K
  • 161
  • 1
  • 4
  • 2
    This sub-item is not in my Project menu. I have Unity 5.5.2 that opened up my VS 2015 Community to create some scripts. Upon adding a default to a property a compiler error showed by the red unline, stating I needed c# 6 as C# v.4 was currently loaded. Anytime I try to select properties of the project the screen just flashes but no page comes up. – Edward Mar 30 '17 at 16:59
15

Check your project properties, go to build, advanced and see if C# 6.0 if you don't have it as default.

Currently there's perfect support for MVC5 and C# 6.0 and works amazingly well!

Bart Calixto
  • 19,210
  • 11
  • 78
  • 114
  • 18
    "Feature 'null propagating operator' is not available in C# 5. Please use language version 6 or greater." didn't know about the Advanced setting but doesn't seem to work here. – juFo Sep 07 '15 at 08:31
  • 3
    How do you change the default version to 6 so that I don't have to set it on each project? – Mir Dec 17 '15 at 20:01
  • 3
    define "some". this does not work in vs 2015 update 3. – Robert Ivanc Sep 08 '16 at 08:00
  • 1
    When I got to project properties, I do not get a build menu. If I go to "Property Pages" I get a build menu, but I do not have an "advanced" button on the build screen. Translation, "This does not work" (for me) – Malcolm Anderson Mar 19 '17 at 16:03
  • My VS version is Community 2015 Update 3 (14.0.25431.01) – Malcolm Anderson Mar 19 '17 at 16:10
  • @MalcolmAnderson it worked for me. Do you have a web site or web application project? I have a web application project. I get the "full-screen" project properties (by right clicking project file and clicking Properties, or by double clicking Properties node in the project itself), then click the Build tab, then click the button "Advanced..." which gives me the C# language selector. Mine was set to 'auto', but I have since changed it. – Derreck Dean Apr 25 '17 at 18:36
  • These options are not available in a Web Site. So it doesn't work for people who aren't using a Web Application. Before someone says "just change to an Application", some of us have legacy applications to support, and aren't allowed to make changes unless requested. – Andrew S Mar 08 '18 at 23:43
12

Including following the advice of installing the latest Microsoft.CodeDom.Providers.DotNetCompilerPlatform I also had to set my root Web.config system.codedom to this to finally get all of the errors in Visual Studio 2015 to go away:

  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.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.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>

Now restart Visual Studio and that should do it.

Serj Sagan
  • 28,927
  • 17
  • 154
  • 183
6

Visual Studio 2015 will also show an Enable C#6 / VB 14 in the Project menu with an ASP.NET web site / web application selected.

This will de facto install Microsoft.CodeDom.Providers.DotNetCompilerPlatform and Microsoft.Net.Compilers packages into your project and add appropriate tags into web.config file.

visual studio 2015 - enable cs6 snipp

Michal Šuvada
  • 170
  • 1
  • 10
  • 1
    what website tab? if you mean properties/web there is no such feature there. – Robert Ivanc Sep 08 '16 at 08:01
  • 1
    I Visual Studio, when you select some WebSite project, the Website tab appears next to View tab. There you may find this feature. – Michal Šuvada Sep 08 '16 at 18:05
  • 1
    I've downvoted for now, please edit to clarify what exactly you mean, e.g. by adding a screenshot. – user247702 Nov 16 '16 at 10:32
  • I'm trying to fix the issue with a Website, and that menu item is not available for me. Note that other non-Web projects in the same solution have no problem using VB 14 features. They all target .NET 4.7, and I installed the CodeDom compiler. – Andrew S Mar 08 '18 at 23:48
-1
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:7 /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.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:15 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
JEuvin
  • 866
  • 1
  • 12
  • 31