How to Enable C# 6.0 Feature in Visual Studio 2013

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


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.

How can I add C# 6.0 to Visual Studio 2013?

The best you can currently do for VS2013 is download the April End User Preview, which is pretty outdated by now.

The VS2013 compiler (as is) doesn't "understand" C#-6 features. Most, if not all of the C# new features are syntactic sugar which the compiler interprets and emits different code for. In order for VS2013 to support that, it has to upgrade the compiler to support those features.

Not to mention VS2015 will bring with it a completely new CSC, named Roslyn

For example, expression body properties:

public override string ToString() => string.Format("{0}, {1}", First, Second);

Compiles down to:

public override string ToString()
{
return string.Format("{0}, {1}", First, Second);
}

C# 6 error messages on VS2013 despite using Microsoft.Net.Compilers nuget package

Even though you install the Microsoft.Net.Compilers package in your project, Visual Studio still uses the C# 5 compiler for the editor. That's why it compiles the C# 6 code, but the editor doesn't understand it. Unfortunately, there is no way to change the compiler editor uses and probably never will be.

So the only way for the full C# 6 support is to upgrade to Visual Studio 2015.

Is there a way to use C# 6 or higher in .NET 4?

The language-version only relies on the version of Visual Studio. To use features intorduced in C#6 you need at least VS2015. This has nothing to do with the installed .NET-framework-version. In fact you can use VS2015 and its compiler-features (e.g. auto-implemented properties with an initial value) and compile against .Net 2.0:

int MyProperty { get; set; } = -1;

To change the target-framework (e.g. .Net 2.0) use Project properties-->Application-->Target framework.

To change the language version via Project properties-->Build-->Advanced-->Language version.

Let´s consider the following example: Beginning with C#3 you can write the following:

var a = new { MyProperty = 1 };

This compiles for any .Net-version. However the most common use-case for anonymous types is when using Linq to fetch data from a database. Linq was introuced in framework-version 3.5 and heavily depends on lambda-expressions and extension-methods, which both were introduced in C#3:

var result = myCollection.Select(x => new { MyProperty = x.MyProperty });

So although you could use anonymous types in earlier versions of the framework, there was little need to do so.

To get a better view on the difference between the C#- (=language)-version and the framework-version read this post. This thread on the other side lists the language-versions and in which version of VS they were released.

How to install the MS C# 6.0 compiler?

From the Roslyn project on GitHub:

To install the latest release without Visual Studio, run one of the following nuget command lines:

nuget install Microsoft.Net.Compilers   # Install C# and VB compilers
nuget install Microsoft.CodeAnalysis # Install Language APIs and Services

To get the latest "preview" drop, published about once per month, add the -pre switch to the nuget commands.


Errors from C# 6.0 syntax

Visual Studio 2013 doesn't support C# 6.0. Visual Studio 2015 (which will RTM on 2015-07-20) does.

Visual Studio 2013 used to support some beta version of Roslyn, but that wasn't updated to the latest versions of Roslyn.

Workaround with Roslyn compiler such that visual studio 2013 understand

To summarize the compatibilities and incompatibilities:

  • C# 6.0 (the language) can be used to compile applications that run .NET 4.0; i.e. it does not require .NET 4.5 or higher.
  • There is no supported way to make Visual Studio 2013 understand C# 6.0 code in the IDE. We did release, as you observe, previews of pre-6.0 things that ran on Visual Studio 2013. Those were just intended to be previews while we finished all the work of making Visual Studio 2015.
  • You can install the NuGet package to make the build use C# 6.0 under Visual Studio 2013, but the IDE will still not understand C# 6.0 features and Intellisense and friends will be broken.
  • To build the Roslyn source code as-is (and of this writing), you need Visual Studio 2015 with Update 1, or equivalent tools for Mac/Linux.

C# 6.0 = Operator

This is an expression-bodied property, a new syntax for computed properties introduced in C# 6, which lets you create computed properties in the same way as you would create a lambda expression. So:

public int TwoTimes(int number)
{
return 2*number;
}

is equivalent to

public int TwoTimes(int number) => 2 * number;

Note: C# 6.0 was introduced with VS 2015. You can not use it with an earlier version.

Ref: What does "=>" operator mean in a property in C#?



Related Topics



Leave a reply



Submit