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

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.

Related

Is Roslyn the default compiler for Visual Studio 2017

Is Roslyn the default compiler in Visual Studio 2017?
I found this article
which tells that Roslyn is not the default compiler and you should install Nuget packages to enable Roslyn.
Nuget packages:
Microsoft.CodeDom.Providers.DotNetCompilerPlatform
Microsoft.Net.Compilers
But I also saw an answer on stackoverflow that says, Roslyn is the default compiler starting from VS 2015.
And when i am install that nuget packages, it's creating a new folder in /bin
with name 'roslyn'
Yes, Roslyn is the default compiler in Visual Studio.
In the article you link, it only says you need to install it separately if you are trying to use it without Visual Studio:
To date, Roslyn has remained a part of Visual Studio 2015 and is installed together with it. Roslyn is a part of Visual Studio 2017 as well. It has been released in March 2017.
However, Roslyn is not included in the .NET Framework. Even in the .NET Framework 4.6 version, the traditional csc.exe and vbc.exe compilers are included. This is done for it to be compatible with previous .NET Framework versions.
To install Roslyn compilers without installing Visual Studio, you need to download and install Microsoft Build Tools. Roslyn can also be downloaded from Github, then you can compile and get binary files csc.exe and vbc.exe, which can be accessed from the command line.
You would typically only need those NuGet packages if you were building an application or service for compiling code (or similar), which is what that article is about. That is, when your application is actually using Roslyn at runtime to process code, rather than itself being built with Roslyn.

System.Runtime.Extensions Injection Errors in Visual Studio 2015

I'm running into an error which seems to be a combination of three factors, and I'm trying to find out if this is a known issue, and if there is a fix or work-around for it.
Factor 1: We are using Visual Studio 2015, update 3.
Factor 2: We are using .net 4.6.2. This version of .net is not included in VS2015, so it was added via the Developer Pack link, located here: https://www.microsoft.com/net/download/windows
Factor 3: We are using Microsoft.Extensions.Dependency Injection. Version 1.0.0, but the issue is reproducible in every version except 2.0.0, which does not appear to be compatible with .net 4.6.2
In order to most easily reproduce this, I am able to create a new solution in Visual Studio 2015, with a new empty web application project, targeting 4.6.2. I open the Nuget package manager, find Microsoft.Extensions.DependencyInjection, and include version 1.0.0 in my project. Build the project, and get this error:
Error CS1703 Multiple assemblies with equivalent identity have been imported: 'c:\Projects\DITest\packages\System.Runtime.Extensions.4.1.0\lib\net462\System.Runtime.Extensions.dll' and 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.6.2\Facades\System.Runtime.Extensions.dll'. Remove one of the duplicate references.
Does anyone have any idea why this might be happening? If I build targeting 4.6.1 it works fine, but we have other dependencies which necessitate 4.6.2. I do not currently have access to Visual Studio 2017, but if it turns out that necessary to make this code run properly, I can pursue it.
Every avenue I've explored regarding this has ended up in a dead-end.

Roslyn: Missing method exception for VSIX package

I have problems since I have updated "Microsoft.CodeAnalysis.CSharp.Scripting" to 1.3.2.
Steps to reproduce:
Create a VS add-in with EditorMargin class.
Change .NET to 4.6
Install Microsoft.CodeAnalysis.CSharp.Scripting and Microsoft.CodeAnalysis.CSharp.
Put somewhere in ctor the following code:
var t = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
After running the add-in it will throw a
Missing method exception
It cannot find a constructor of CSharpCompilationOptions. I noticed that when I don't install "Microsoft.CodeAnalysis.CSharp.Scripting" (which is not necessary in the above case), everything works. I guess one of dependencies of Microsoft.CodeAnalysis.CSharp.Scripting installs some packages.
I tried to run "Fuslogvw" to see if there are any binding errors but I could not find anything useful.
Any ideas?
Update
- I use Visual Studio 2015 update 3
Update 2
I downgraded "System.Collections.Immutable" from 1.2.0 to 1.1.37 and it works again.
Update 3
I found the reported bug:
https://github.com/dotnet/roslyn/issues/12247
The problem is I need to use 1.2.0 because Roslyn scripting API uses that version.
The current workaround for me is to downgrade Microsoft.CodeAnalysis.Csharp.Scripting to the version which works with "System.Collections.Immutable 1.1.37"
If you're targeting package version 1.3.2, that means you're targeting Visual Studio 2015 Update 3, but I'm guessing you don't have that installed. Either downgrade to an older version (i.e. 1.2 if you're targeting Update 2, 1.1 if targeting Update 1), or upgrade your VS to a newer version.
I was able to resolve this problem installing Visual Studio 2017. VS 2017 requires System.Collection.Immutable 1.2.1.0, which does not conflict with other Roslyn versions

How to upgrade msbuild to C# 6?

I want to use C# 6 in my project (null propagation, other features).
I've installed VS 2015 on my PC and it works brilliantly and builds test code like
var user = new SingleUserModel(); //all model fields are null
var test = user.User?.Avatar?["blah"];
But when I push my project to the repo and CI starts to build it, build fails because of unsupported ?.
I've installed VS2015 on CI server too but looke like it doesn't use it.
What can I do?
CI - CruiseControl .NET
Builds with C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe
Make sure you call:
C:\Program Files (x86)\MSBuild\14.0\Bin\MsBuild.exe
That's the version of MsBuild that ships with Visual Studio 2015 and calls the C# compiler that understands this. You can get this version of MsBuild on your system by installing any edition of Visual Studio 2015 or by installing the stand-alone Microsoft Build Tools 2015.
Adding a reference to the following NuGet package will also force use of the new compiler:
Install-Package Microsoft.Net.Compilers
Please note Install-Package will pick the latest available version which may not be the one you are looking for. Before you install, please check the release notes and dependencies to resolve the underlying issue with the version being dealt with, which in this case, was more specific to VS 2015.
So for Visual Studio 2015:
Install-Package Microsoft.Net.Compilers -Version 1.0.0
You can by the way also install the "Microsoft Build Tools 2015" instead of VS2015 on your build server.
https://www.microsoft.com/en-us/download/details.aspx?id=48159
It installs MSBuild to the same path:
C:\Program Files (x86)\MSBuild\14.0\Bin\MsBuild.exe
You probably already have this working, but this might help someone else in the future. I came across this question recently and it got me moving in the right direction and ultimately led to a solution.
Another possible solution to this is manually updating your project files to target the MSBuild version you want your projects to be built with.
I've recently gone through a TeamCity build server update and I've already installed the Microsoft Build Tools 2015 on it. I thought I had everything in place on the build server, I had my solution targeting C# 6.0, and I had every project targeting .net 4.6.1. Like you, everything with C# 6.0-specific code built just fine in my local environment, but my TeamCity build server didn't like any of it.
As mentioned by others, I tried using the Microsoft.Net.Compilers NuGet package. The latest version of it allowed the build to work on my build server, but it wouldn't let me publish my code locally (a requirement of mine). Earlier versions of that NuGet package would let me publish, but the build wouldn't work.
What I found that I had to do was ultimately modify each project file in my solution to specifically target the MSBuild version that could handle C# 6.0 code. In each of my project files, I found a line similar to the following line:
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
with the key component of that line being the ToolsVersion portion of it. I simply changed this line on my project files to read the following:
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
The difference here was that I was targeting version 14, not 4. Version 14.0 corresponds with Build Tools 2015. By changing this, my TeamCity build server used the correct MSBuild version and was able to build my C# 6.0 code.
I also had to manually update the TargetFrameworkVersion xml node of this to use 4.6.1 because VS2015 wasn't doing something right and messed up my local build, but that's not relevant here.
Please, someone correct me if I'm wrong, but just for reference, I think the version numbers go something like this:
4.0 = VS2012
12.0 = VS2013
14.0 = VS2015
15.0 = VS2017
I believe if you wanted to use .net 4.7, you'd have to have the Build Tools 2017 installed and have your projects targeting 15.0 instead of 14.0, but I haven't verified this.

Error in Visual Studio 2013 when including JetBrains ReSharper via NuGet

When I include JetBrains-ReSharper my project using NuGet Package Manager, and then I try to rebuild the project, it shows an error.
Error 14 The type 'System.Threading.LazyInitializer' exists in both 'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\mscorlib.dll' and 'Project\packages\JetBrains.ReSharper.SDK.8.2.1158\bin\System.Threading.dll' Project\Filters\InitializeSimpleMembershipAttribute.cs
Can anybody can help me to solve the problem ?
Sadly, because ReSharper is a .net 3.5 application, and due to the way the SDK is set up, it includes references to the .net 3.5 compatible System.Threading.Tasks.dll back port Microsoft initially released with RX. The unfortunate part is that this file is referenced even if your plugin is a .net 4 project, and so you get conflicts with the real System.Threading.Tasks.
You can change your project to be .net 3.5, but then (again, due to the way the SDK is set up) you'll get other warnings about .net 4 assemblies that are referenced, but shouldn't be. Essentially, you just have to ignore those warnings. We're working on fixing all of this for 9.0.
However, as #derigel mentions in the comments - adding the ReSharper SDK to an MVC project is a little weird, and frankly, won't work. The ReSharper SDK is for building ReSharper plugin extensions. If you want to install ReSharper, download it from here: http://www.jetbrains.com/resharper/download/

Categories