200

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.

Carthic
  • 93
  • 9
Hassaan
  • 3,931
  • 11
  • 34
  • 67

6 Answers6

176

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.

David De Sloovere
  • 3,415
  • 2
  • 24
  • 27
  • I see autoinitialized props works, however primary constructor does not – Philipp Munin Sep 10 '15 at 00:37
  • Yes. This is a working solution if you don't have the option go to VS2015. It will allow you to build, but it is quite hard with a IDE they doesn't really work. My opinion: use VS2015. It was built for the job. – Patrick Hofman Sep 11 '15 at 21:06
  • 8
    Primary constructors were actually removed from the final release. That's why they don't work. – Mark A. Donohoe Sep 16 '15 at 03:32
  • This is a good backup in cases of emergency problems with VS 2015, as we have: Azure SDK refuses to update credentials, so Server/Cloud Explorers are completely dead. –  Oct 08 '15 at 12:47
  • 8
    Just want to point out for anyone wanting to use C#6 in Razor in an MVC5 project, looks like that's a separate NuGet package, and required even if you're using VS2015. See http://stackoverflow.com/a/30832849/11398 – Jon Davis Oct 09 '15 at 18:28
  • Great solution for enterprise environments where you have some developers testing VS2015 and others still on VS2013. – ammills01 Feb 02 '16 at 17:27
  • 2
    It works (as in you can compile), however VS2013 still shows errors: http://stackoverflow.com/questions/35796001/c6-error-messages-on-vs2013-despite-using-microsoft-net-compilers-nuget-package – mnwsmit Mar 04 '16 at 14:43
  • Works very nice! Is there also a way to install that package machine-wide? – Falco Alexander Apr 05 '16 at 16:18
  • 2
    he he he. This was cool. Compiler shows errors in advance but compiles successfully when actually compiled. What a hack to start a weekend morning! – RBT May 20 '16 at 00:11
  • Note that the **build time will be increased for VS 2015 users** after adding Microsoft.Net.Compilers package –  May 24 '16 at 08:33
  • 1
    **it will show errors in the code editor window although it will build fine.** Just felt the need to point it again. Great answer! – meJustAndrew Sep 25 '16 at 15:13
  • This didn't work for me on VS 2013. I had to install an older version of there is an answer by SteveCinq that worked, basically installing older version of compiler. I'm not sure what the negative consequences might be- but I only needed to compile this one program. – Rich Bianco May 29 '18 at 16:03
64

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.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • yeah same line which i read. Just thinking of getting vs 2015 preview – Hassaan Nov 23 '14 at 20:38
  • 7
    unfortunatily, the end user preview for vs 2013 has been closed :( – Hassaan Nov 23 '14 at 20:50
  • The preview is still available here: https://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=52793 – Erwin Mayer May 03 '15 at 17:05
  • It is. But it isn't updated any more. See the last update date. – Patrick Hofman May 03 '15 at 18:07
  • 4
    Just to make sure, there is no a supported way to write in C# 6 using VS 2013? Do I have to use VS 2015? – vorou Jul 21 '15 at 13:19
  • 2
    That is correct. @vorou There is no recent version of the preview for VS 2013. – Patrick Hofman Jul 21 '15 at 13:20
  • 5
    Actually, these is a way (see my answer). But it's different than what you could do months ago before the final release. – David De Sloovere Aug 14 '15 at 12:55
  • 1
    none of the solutions in this topic or other similar topics helped me. so the only work around is moving to VS 2015 I guess. – Yar Jan 21 '16 at 15:13
  • I was having this problem IN VS2015 and it appears the issue was my CSPROJ files pointing to an old ToolsVersion and since I had that old .NET version it would compile using it. This is why it worked for others, but did not work for me. – nicmoon Dec 11 '17 at 20:22
5

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.

SteveCinq
  • 1,920
  • 1
  • 17
  • 22
  • @RichBianco That's great - thanks for the feedback and up-vote. Personally, I can't understand why v2.0.1 doesn't work. If anyone has any clues about this - as in, getting rid of the error I reported - please post your fix. – SteveCinq May 26 '18 at 22:58
4

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

2

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:

  1. Unit Tests in Resharper don't work for me with Visual Studio 2015 for some reason
  2. 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.

devdimi
  • 2,432
  • 19
  • 18
-1

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.

Alexander Christov
  • 9,625
  • 7
  • 43
  • 58
  • 1
    the link gives me 'not authorized' even after ms login. Of course, install VS2015 or later is a £XXX,000s expense in some settings. – Chris F Carroll Oct 25 '17 at 10:56