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.
Default Microsoft Windows hidden shares C$ and x$ - The default drive share, by default C$ is always enabled. The x$ represents other disks or volumes that are also shared, e.g., D$, E$, etc. FAX$ - Share used by fax clients to access cover pages and other files on a file server.
In website's NuGet window:
Microsoft.CodeDom.Providers.DotNetCompilerPlatform
web.config
,
under: system.codedom > compilers > compiler
,
change compilerOptions
="/langversion:6 to 7
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+).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With