C# 6.0 Features Not Working with Visual Studio 2015

C#asp.net MvcVisual Studio-2015RoslynC# 6.0

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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/

Solution 2 - C#

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" 

Solution 3 - C#

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.

Solution 4 - C#

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!

Solution 5 - C#

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.

Solution 6 - C#

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

Solution 7 - C#

<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+" />

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris SchiffhauerView Question on Stackoverflow
Solution 1 - C#jbtuleView Answer on Stackoverflow
Solution 2 - C#Sergey KljopovView Answer on Stackoverflow
Solution 3 - C#Tom KView Answer on Stackoverflow
Solution 4 - C#Bart CalixtoView Answer on Stackoverflow
Solution 5 - C#Serj SaganView Answer on Stackoverflow
Solution 6 - C#Michal ŠuvadaView Answer on Stackoverflow
Solution 7 - C#JEuvinView Answer on Stackoverflow