Enabling c# 7 in a asp.net application

enable c# 7 in visual studio 2015
c# 7.1 visual studio 2017
.net core 2.2 c# version
c# language version
c# version in visual studio 2017
how to check c# version in visual studio 2017
latest c# version
c# version in visual studio 2019

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.

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 compilerOptions="/langversion:6 to 7

Enabling C# 7 Compilation With Visual Studio 2017, The rules in this article apply to the compiler delivered with Visual Studio 2019 or the .NET NET Core SDK versions target C# 7.0 by default. If it puts a red line under “private protected” saying that this access modifier combination is not allowed then you’ll know that C# 7.2 has not been enabled yet. First of all make sure that you have the latest .NET SDK installed on your computer: .NET SDKs for Visual Studio. This is 4.7.1 at the time of writing this post.

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

But below setting gives compile time error:

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__

Update

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

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+).

C# language versioning, The c# application projects now default to the c# 7.0 compiler. I cannot find a way to enable c# 7.0 in the asp.net web projects though. This article explains how to enable C# 7 (C# language version 7) compilation with Visual Studio 2017. You may be thinking that C# 7 features are already supported with Visual Studio 2017 and you do not need to make any changes for that. Yes, you are almost right but this is not 100% true. Wait for a while and let me explain.

If you try to install Microsoft.CodeDom.Providers.DotNetCompilerPlatform version 2.0.0 and your project targets a version of .net older than 4.6, then it will automatically use an older version of roslyn which only supports up to langversion 6. This is because newer versions of roslyn, including the first version to support csharp-7, require at least .net-4.6 to run. If your project targets an older version of .net, you will get the error message you see:

CS1617 Invalid option 'latest' for /langversion; must be ISO-1, ISO-2, Default or an integer in range 1 to 6.

  1. Ensure that your project is targeting at least .net-4.6. Retarget if necessary.
  2. If your project still uses packages.config, then you must uninstall and reinstall Microsoft.CodeDom.Providers.DotNetCompilerPlatform to update your project file to point to the .net-4.6 variant of the nuget package. If you are using <PackageReference/>, you are all set (but you have to manually configure web.config’s system.codedom section).

New C sharp 7 features in action: Migrate existing .NET 4 or .NET , NET 4 or .NET 4.5 Application to VS 2017 to support/enable C sharp 7. Step 2: Update Nuget package to support/enable C# 7. Existing packages that Part of the top 1% Stack Overflow answerers in ASP.NET technology. Open Visual Studio 2017 (version 15.3.5 or later) => File => New Project => Web => ASP.NET Core Web Application. Select .NET Framework 4.7 & ASP.NET Core Web Application, as shown in the following screenshot. Click on OK button. It opens a new window where you can choose different versions of ASP.NET Core.

Using C# 6.0 or C# 7.0 with MVC 5, Using C# 6.0 or C# 7.0 with MVC 5. 28 April 2017 on asp.net-mvc, c#, csharp, razor. The version of C# that your solution or project employs is fairly abstracted. ASP.NET includes a full runtime compilation pipeline that compiles many of your application’s assets on the web server when the application is running (hence “runtime compilation). Even if you’re using the project compilation model detailed above, part of your application will be compiled using ASP.NET’s runtime compilation feature.

I was referencing a custom Project A that was referencing another custom Project B. I just readded the references from A to B and it seemed to work (for now).

4 ways to enable the latest C# features, By default, the version of C# is the latest major version (currently 7.0). 4 ways to enable the latest C# features. 08/24/2017. Gérald Barré .NET. The C#  Enable ASP.NET Functionality for a Virtual Server By default, ASP.NET functionality is enabled for IIS virtual servers after the .NET Framework is installed. However, if you disable ASP.NET, you may want to reenable it. Or, you may want to enable ASP.NET for a virtual folder on a virtual server that has ASP.NET disabled.

How To Enable C# 7.1 Version To My Projects, language may have features that rely on types or runtime components that are not available in every . NET implementation. Enable cross-origin requests in ASP.NET Web API 2. Browser security prevents a web page from making AJAX requests to another domain. This restriction is called the same-origin policy, and prevents a malicious site from reading sensitive data from another site.

Visual Studio–How to target different/latest c# version (.NET Core 3 , How do I know what version of C# I am using? Adding an ASP.NET application with the command line; In Plan an ASP.NET Website on IIS, you made the following decisions: Alias string that becomes part of the application root URL. Application pool to run the application in. Physical path to the application files on the server. To add an ASP.NET application by using the UI. Open IIS Manager.

Choosing the C# Language latest version (minor release ) in Visual , Build highly performant, multi-threaded, and concurrent applications using C# 7 and . Enabling. CORS. in. the. ASP.NET. Core. application. The following is a  When the New Project dialog box is displayed, select Installed and expand Visual C#. Under Visual C#, select Web. In the list of project templates, select ASP.NET Web Application (.Net Framework). Enter a name for your project and click OK. When the New ASP.NET Project is displayed, select the Single Page Application template and click Create

Comments
  • did you try changing compilerOptions ? msdn documentation state it affects just that. msdn.microsoft.com/en-us/library/f4ckecs0.aspx
  • I tried setting it to /langversion:7. Does not compile.
  • It returns: CS1617: Invalid option '7' for /langversion; must be ISO-1, ISO-2, Default or an integer in range 1 to 6.
  • 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/…
  • Julien, I wish Microsoft provided easier tools, but now they have two packages available through nuget: Microsoft.Net.Compilers, which has just been updated to v2.0.1, and Microsoft.CodeDom.Providers.DotNetCompilerPlatform compiler as a service (Roslyn), which is in v1.0.3 (released last December). Should I assume that c# 7 is not yet ready for asp.net?
  • Thank you for your answer. That did the trick. This is the correct solution.
  • 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
  • None of these suggestions work for me - not this or previous answer.
  • There is new version of Microsoft.CodeDom.Providers.DotNetCompilerPlatform (1.0.4) which enables C# 7 on web project. The project has to target to 4.6+, since Roslyn compiler 2.0 need .net framework 4.6.
  • Also possible: /langversion:latest, which is similar to /langversion:default. See documentation).
  • 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, 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.
  • 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.
  • 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.