How to set the .NET Version for VisualStudio2015 (Code) - c#

Some people in our team are using VisualStudio 2015 while the rest is still using 2013 (both with ReSharper 9.1).
The Target Framework in the project properties is set to .NET Framework 4.5.1.
My Problem: I can still use code like
public int X() => x;
which is a .NET 4.6 feature. When I build the project, it also runs (I guess because it's more or less syntactical sugar, so the compiler makes code that doesn't require .NET 4.6).
My colleagues however are not very amused, when they check out my changes in Visual Studio 2013 ;-)
Is it possible to get warnings / compile errors in Visual Studio 2015 for using .NET 4.6 features?

That is an expression-bodied member and it is a new language feature introduced in C# 6.0.
The language and the framework/runtime libraries are versioned separately.
What you really want to do is change the language version.
In the Solution Explorer, right-click on the project name, then click the menu item entitled Properties
Click on the Build tab, and then the Advanced button. A window should appear.
From the Language Version dropdown, choose C# 5.0.
Hit the OK button, then re-build.

Related

Visul Studio 2017 error "Failed tp complile the project in memory" Please use Language Version 7.3 or greater

I have a project that is written using C# on the top of ASP.Net Core 2.2 framework. This project started at ASP.NET Core 2.1 and was upgraded to 2.2.
This project contains code that is utilizing some of the latest features in C# compiler 7.3 which is required for my project to compile. To force my project to use C# compiler 7.3, I added the following to my .csproj file
<PropertyGroup>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
Note that changing <LangVersion>7.3</LangVersion> to <LangVersion>latest</LangVersion> will not work for some reason the project will fail to compile. It's like Visual Studio thinks 7.3 is not the latest or is not aware of its existence.
The above code makes the project compile using the latest C# compiler 7.3. However, I am now trying to scaffold Identity items using the following steps
From Solution Explorer, right-click on the project > Add > New Scaffolded Item.
From the left pane of the Add Scaffold dialog, select Identity > ADD.
In the ADD Identity dialog, choose files you wish to override.
Select layout if necessary
Select or create Data context class.
Hit ADD.
But that is giving me the following error "Failed to compile the project in memory"
It seems as Visual Studio 2017 is trying to compile the project in the memory during the scaffolding process using C# compiler 7.0 instead of C# compiler 7.3 which is why I am getting this error. Keep in mind that my project will not compile unless C# compiler 7.3+ is used.
How can I force Visual Studio 2017 to use C# compiler 7.3 everywhere in my project and in the memory?
I know its a bit late, but the error is clear. You're using enum generic constraints in two clases.
EnumMenuViewModel, line 7, column 105.
EnumOptionalMenuViewModel, line 6.
The scaffolding compiles the project using C# 7.0, and this feature is not available.
You can disable these constraints, run the scaffolding, then enable it again.
I hope it helps someone.

In Which .Net Framework Version C# 7.2 is Available

I tried installing .net framework 4.7.2, still the target framework is empty in the project properties. The program I'm trying to run uses readonly struct, which is part C# 7.2.
Could you please guide me .Net Framework version has C# 7.2?
Language features are independent of .Net version (Framework/Core/Standard).
You will need to use a compiler that understands the newer language features. Assuming you're using Visual Studio 2017, you should update to latest version.
By default C# projects in Visual Studio use the latest major release of the language (which would be 7.0 as of this writing). To change this, open up the project's properties, select the Build page, click Advanced..., and under General set Langauge Version to "C# 7.2".
Per phuzi's comment mentioning this question, you might need to update to the latest Visual Studio in order to see this option.

Is StyleCop 4.7 compatible with C# 6.0 code

I installed StyleCop 4.7 for developing customized rules, to analyze our C# code.
We write the code in Visual Studio 2015. I can analyze my C# 5.0 (Visual Studio 2013) code, and I get correct response, but when I analyze my C# 6.0 (Visual Studio 2015) code, StyleCop raises an exception for my rules.
Now my question is:
Is StyleCop 4.7 compatible with C# 6.0 code? If not, do we have any solution?
TL;DR - Yes. StyleCop 4.7 was updated to be compatible with C# 6 starting with 4.7.51 (Beta) released on 13th March 2016.
There was a handover to new custodians back in January 2016, and a new version 4.7.50 alpha has been released 3rd Feb 2016, but as yet does not include C# 6 support.
4.7.51 (Beta) was released on 13th March 2016 and the latest stable is 4.7.54 released May 13th.
Here are three other options that work with Visual Studio 2015:
1. Visual StyleCop
Visual StyleCop, is an extension to Visual Studio. This gives you the right click functionality into Visual Studio 2015 that installing StyleCop gave you for 2013 and below.
This is available by adding an extension into Visual Studio:
It is an active project and will give you StyleCop on C# 6 features. There is a package - Visual-StyleCop.MSBuild, that will give you MSBuild integration.
It is possible to write custom rules with Visual StyleCop.
2. StyleCop Analyzers
StyleCop.Analyzers are built upon Rosalyn and make use of the Analyzers feature of Visual Studio 2015. They are available on NuGet:
This would add them into the rules in the relevant *.ruleset file (same place as CodeAnalysis rules, accessible from the project page:)
giving:
and you can run them via
Which has the same effect as right click, Run StyleCop, used on previous versions and with Visual StyleCop.
Giving:
The source code is available on GitHub, so adding custom rules should be possible.
3. Resharper, with StyleCop by Jetbrains
Install:
Resharper 10.0.1
StyleCop by JetBrains 4.8
Which gives this in R# options:
This gives you syntax highlighting, but no right click run, and no build integration. Additionally, R# is not a free product.
You can extend it using plugins, but it doesn't feel as customizable as the other two options presented.
Version 4.7.51 and later should support C# 6:
Added initial (and untested) support for C# 6.0 syntax
http://stylecop.codeplex.com/releases/view/620339
So the current stable build should support it, too: http://stylecop.codeplex.com/releases

How to enable C# 6.0 feature in Visual Studio 2013?

I was going through the latest features introduced in C# 6.0 and just followed an example of auto property initializer,
class NewSample
{
public Guid Id { get; } = Guid.NewGuid();
}
but my IDE did not recognize the syntax.
I am wondering how I could enable C# 6.0 in Visual Studio 2013. The Target framework I am using is 4.5.1.
Under VS2013 you can install the new compilers into the project as a nuget package. That way you don't need VS2015 or an updated build server.
https://www.nuget.org/packages/Microsoft.Net.Compilers/
Install-Package Microsoft.Net.Compilers
The package allows you to use/build C# 6.0 code/syntax. Because VS2013 doesn't natively recognize the new C# 6.0 syntax, it will show errors in the code editor window although it will build fine.
Using Resharper, you'll get squiggly lines on C# 6 features, but the bulb gives you the option to 'Enable C# 6.0 support for this project' (setting saved to .DotSettings).
As mentioned by #stimpy77: for support in MVC Razor views you'll need an extra package (for those that don't read the comments)
Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
If you want full C# 6.0 support, you'll need to install VS2015.
Information for obsoleted prerelease software:
According to this it's just a install and go for Visual Studio 2013:
In fact, installing the C# 6.0 compiler from this release involves little more than installing a Visual Studio 2013 extension, which in turn updates the MSBuild target files.
So just get the files from https://github.com/dotnet/roslyn and you are ready to go.
You do have to know it is an outdated version of the specs implemented there, since they no longer update the package for Visual Studio 2013:
You can also try April's End User Preview, which installs on top of Visual Studio 2013.
(note: this VS 2013 preview is quite out of date, and is no longer updated)
So if you do want to use the latest version, you have to download the Visual Studio 2015.
A lot of the answers here were written prior to Roslyn (the open-source .NET C# and VB compilers) moving to .NET 4.6. So they won't help you if your project targets, say, 4.5.2 as mine did (inherited and can't be changed).
But you can grab a previous version of Roslyn from https://www.nuget.org/packages/Microsoft.Net.Compilers and install that instead of the latest version. I used 1.3.2. (I tried 2.0.1 - which appears to be the last version that runs on .NET 4.5 - but I couldn't get it to compile*.) Run this from the Package Manager console in VS 2013:
PM> Install-Package Microsoft.Net.Compilers -Version 1.3.2
Then restart Visual Studio. I had a couple of problems initially; you need to set the C# version back to default (C#6.0 doesn't appear in the version list but seems to have been made the default), then clean, save, restart VS and recompile.
Interestingly, I didn't have any IntelliSense errors due to the C#6.0 features used in the code (which were the reason for wanting C#6.0 in the first place).
* version 2.0.1 threw error The "Microsoft.CodeAnalysis.BuildTasks.Csc task could not be loaded from the assembly Microsoft.Build.Tasks.CodeAnalysis.dll. Could not load file or assembly 'Microsoft.Build.Utilities.Core, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.
UPDATE One thing I've noticed since posting this answer is that if you change any code during debug ("Edit and Continue"), you'll like find that your C#6.0 code will suddenly show as errors in what seems to revert to a pre-C#6.0 environment. This requires a restart of your debug session. VERY annoying especially for web applications.
It worth mentioning that the build time will be increased for VS 2015 users after:
Install-Package Microsoft.Net.Compilers
Those who are using VS 2015 and have to keep this package in their projects can fix increased build time.
Edit file packages\Microsoft.Net.Compilers.1.2.2\build\Microsoft.Net.Compilers.props and clean it up. The file should look like:
<Project DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
</Project>
Doing so forces a project to be built as it was before adding Microsoft.Net.Compilers package
It is possible to use full C# 6.0 features in Visual Studio 2013 if you have Resharper.
You have to enable Resharper Build and voilá!
In Resharper Options -> Build - enable Resharper Build and in "Use MSBuild.exe version" choose "Latest Installed"
This way Resharper is going to build your C# 6.0 Projects and will also not underline C# 6.0 code as invalid.
I am also using this although I have Visual Studio 2015 because:
Unit Tests in Resharper don't work for me with Visual Studio 2015 for some reason
VS 2015 uses a lot more memory than VS 2013.
I am putting this here, as I was looking for a solution for this problem for some time now and maybe it will help someone else.
It seems there's some misunderstanding. So, instead of trying to patch VS2013 here's and answer from a Microsoft guy: https://social.msdn.microsoft.com/Forums/vstudio/en-US/49ba9a67-d26a-4b21-80ef-caeb081b878e/will-c-60-ever-be-supported-by-vs-2013?forum=roslyn
So, please, read it and install VS2015.

How can Resharper be made aware of the framework version?

I am coding in VS2008 with Resharper 4.5.1, but the projects are set to target .NET Framework 2.0.
Still, Resharper is making suggestions that are relevant to the .NET 3.5 framework. For instance, it tells me that I should be using collection initializers, etc...
I've looked through the settings and can't seem to find the checkbox to tell it to give 2.0 specific advice.
Select your project in the Solution Explorer and open the Properties tool window (F4 in the standard keyboard layout or View > Properties Window after selecting the project). In the ReSharper section, there is a Language Level property that you can set to C# 2.0. Note that there are two separate project properties windows that manage different properties, if you see tabs for "Application", "Build" and "Debug" you are in the wrong window.
As others have said, this affects the version of C#, not the version of the framework (since most of the C# 3.0 changes can be compiled to an assembly that targets .NET 2.0).
Those features are not .NET 3.5 framework features, but merely features of the 3.5 compiler. And since in VS2008 this is the compiler invoked for .NET 2 targets, it does handle these syntax extensions correctly.

Categories