I just started working on my old solution in Visual Studio 2017. Just opening the solution in the old IDE worked seamlessly. The c# application projects now default to the c# 7.0 compiler. The property pages of those project (compilation/advanced) let easily chose the targeted language version of the compiler, defaulting to the latest.

I cannot find a way to enable c# 7.0 in the asp.net web projects though. If I write a statement such as:

if (int.TryParse("1", out int myInt)) { ... }

the IDE warns me saying that I need to use version 7+ of the language.

My research on this topic shows I should target the specific c# version in the system.codedom compilers area of the web.config file, so to target the newest Roslyn version.

What I have now is:

  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>

which targets c# 6. What are the correct settings for c# 7, provided that I have already downloaded the latest Roslyn with nuget?

Update Here is a screenshot of the available Compile options for a web project (it is Italian VS2017 but it should be easy to understand). No possibility to select the targeted c# version there. Compile options

share|improve this question
    
did you try changing compilerOptions ? msdn documentation state it affects just that. msdn.microsoft.com/en-us/library/f4ckecs0.aspx – Stavm Mar 12 at 7:27
    
I tried setting it to /langversion:7. Does not compile. – davidthegrey Mar 12 at 8:00
    
It returns: CS1617: Invalid option '7' for /langversion; must be ISO-1, ISO-2, Default or an integer in range 1 to 6. – davidthegrey Mar 12 at 8:17
    
in C#6 microsoft added a submenu to the visual studio named "Project" > "Enable C#6" check if they did the same with vs2017, blogs.msdn.microsoft.com/webdev/2015/12/07/… – Stavm Mar 12 at 15:22
    
No, there is not such item in the Project menu for web applications. If they put this option anywhere, it is hidden very well. – davidthegrey Mar 12 at 17:37

In Website's NuGet window:

  1. Uninstall Microsoft.CodeDom.Providers.DotNetCompilerPlatform
  2. Re-install it
  3. In web.config under: system.codedom > compilers > compiler Change the compilerOptions="/langversion:6 to 7
share|improve this answer
    
Thank you for your answer. That did the trick. This is the correct solution. – davidthegrey Mar 13 at 14:45
    
You are welcome – Hassan Abdullah Mar 14 at 8:02
3  
This absolutely works and is the answer! But for completeness sakes for those come here, the OP said assuming latest rosyln installed with nuget. That means installing Microsoft.Net.Compilers 2.0<=. Also if you want to use C# 7 tuples you need to install from nuget System.ValueTuple – jbtule Mar 20 at 21:20

I am able to compile it with default language setting but not with C# 7 option. enter image description here

But below setting gives compile time error:

enter image description here

so you can keep your language version setting as default.

If you experimenting with Roslyn and not using Visual 2017 default compiler build then you may need to make some more changes

Select your project name and right click >> Properties Window >> Build and then add the below two options in "Conditional Compilation symbols" text box DEMO,DEMO_EXPERIMENTAL

enter image description here

Update

In order to use C# 7.0, you need to use 2.0+ version of Microsoft.Net.Compilers

enter image description here

after installing the latest version of Microsoft.Net.Compilers (2.0+) you can select the language version as C# 7.

so the best solution is to install the latest version of Microsoft.Net.Compilers (2.0+).

share|improve this answer
    
Banketeshvar, the "Advanced Build Settings" dialog is available only for desktop and console applications. Not for web projects. In web projects, you have to manually edit the web.config file. I have downloaded version 2 of the compilers, but setting /langversion:7 still yelds to the compiler error. – davidthegrey Mar 13 at 7:51
    
@davidthegrey, I do not think that language version has to do anything with the console or web App. I am able to open Advanced Build setting option with ASP.NET MVC App (both Native framework and .NET Core). I have compiled ASp.NET Core Web App as well with C# 7.0 language setting option. – Banketeshvar Narayan Mar 13 at 13:04
    
Are you using the latest version of Visual Studio 2017? I have downloaded the latest version of Visual Studio 2017 Enterprise which was released on 7th march 2017. for ASP.NET Core Web Application I have set the language version using the following steps: Right Click on ASP.NET Core Project --> Select properties from Context menu --> Go to Build Tab --> Click on "Advanced..." button --> It will open the "Advanced Build Setting" Window. The same thing also working for me for ASP.NET MVC projects with .NET Native framework. – Banketeshvar Narayan Mar 13 at 13:12
    
I think you must be aware that we need to install compilers for each project separately from Nuget. Could you please give some more details that what type of project you have created earlier with which version of visual studio and now which edition of Visual Studio 2017 you are using. I want to know the preceding info to replicate the issue at my end. – Banketeshvar Narayan Mar 13 at 13:19
    
I am on Visual Studio 2017 Professional RTM. I have added a screenshot of the available Compile options for web projects in my original post. And yes, I am aware that every project in the solution use independent compiler settings. – davidthegrey Mar 13 at 14:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.