My current work project uses the .NET framework v4.5, and we’re developing using Visual Studio 2013. I’ve been reading about the new language features of C# 6.0 for a while, and I’m really interested in finding a way to use them.

I used Bing/Google to identify what the new language features are, read some blogs and fired up my personal copy of Visual Studio 2015 to try out a few examples. The one I was really interested in was Primary Constructors, but there were a list of new features I wanted to try:

  • Primary Constructors
  • Import of static type members into namespace
  • Auto property initializers
  • Default values for getter-only properties
  • String interpolation
  • nameof operator
  • Dictionary initializer
  • Null propagator

This isn’t a comprehensive list of the new features, they’re just the ones that I think I would use most in my day-to-day coding.

The first feature I decided to try was creating a Primary Constructor…but when I wrote the code in VS2015 and .NET 4.6, it showed the dreaded red squiggly line and didn’t compile. What went wrong?

After some more research, I found that the Primary Constructor feature has been removed (at least temporarily). So those articles (for example, this one) showing me how to do this are, for now, wrong.

This made me sit back and think a bit more.

  • When I look at the dropdown list of available .NET frameworks in Visual Studio 2015, there’s quite a few (at least in my system). Which one should I be using to compile C# 6.0 language features?
  • And what does C# 6.0 actually mean? Should I be assuming that .NET Framework 4.6 necessarily corresponds to C# 6.0?
  • Can I make Visual Studio 2013 compile code written using C# 6.0 language features?
  • And where does Roslyn fit into all of this?

Some Sample Code for C# language features

I wrote a simple class which contains each of the C# 6.0 features that I listed above (except Primary Constructors, obviously). It’s a bit of a silly example, but hopefully it illustrates the point. I’ve commented each of the features, and put some of the most relevant code in bold font.

namespace CSharp6SampleApplication
{
    using System;
    using System.Collections.Generic;
    using static System.Console;

    public class SuperCar
    {
        // Dictionary initializer
        private static readonly Dictionary<string, DateTime> _specialDates =
            new Dictionary<string, DateTime>
            {
                ["past"] = new DateTime(1985, 10, 26),
                ["current"] = new DateTime(1985, 11, 5),
                ["future"] = new DateTime(2015, 10, 21)
            };

        // Auto property initializers
        public string Manufacturer { get; set; } = "DeLorean";

        // Auto property initializers
        public int TopSpeed { get; set; } = 88;

        // Default values for getter-only properties - no need to specify a private setter;
        public double Power { get; }

        public Engine Engine { get; set; }

        public SuperCar()
        {
            // Default values for getter-only properties - possible to set in the constructor only
            Power = 1.21;
        }

        public override string ToString()
        {
            // String interpolation
            return $"Made by {Manufacturer}, Top Speed = {TopSpeed}";
        }
    }

    public class Engine
    {
        public string Manufacturer { get; set; }

        public bool IsEfficient(string engineType)
        {
            // nameof operator
            if (engineType == null)
            {
                throw new ArgumentNullException(nameof(engineType));
            }

            if (engineType == "Mr. Fusion")
            {
                return true;
            }

            return false;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var car = new SuperCar();

            // Import of static type members into namespace
            WriteLine(car.ToString());

            // Null propagator
            WriteLine(car.Engine?.Manufacturer ?? "No engine type specified yet");
        }
    }
}

How it works

There’s a difference between a language specification and the version of the framework that supports it.

C# 6.0 is a language specification that is supported by the Roslyn compiler for the .NET platform. This compiler ships by default with Visual Studio 2015 – however, Roslyn doesn’t ship with Visual Studio 2013 (obviously, because this came out before Roslyn).

So all of the above code will compile and works out of the box in Visual Studio 2015, and it works for .NET framework versions 2.0, 3, 3.5, 4, 4.5, 4.5.1, 4.5.2, and 4.6 (I just haven’t included version 1.0 and 1.1 because I don’t have them installed on my machine). It doesn’t matter about the framework – it’s the compiler that matters.

Can it work in VS2013?

I think the answer to this is “partly but not really”.

When I try the above code in VS2013, the environment looks like the screenshot below, with more of the red-squiggly lines and a bunch of compiler errors.

vs2013_code

But it’s possible to compile C# 6.0 features with Visual Studio 2013 – you just need to install a nuget package. Run the code below from the package manager console.

Install-Package Microsoft.Net.Compilers -Version 1.1.1

This will now compile, but the VS2013 development environment still thinks there’s a problem – it leaves the red squiggly lines and reports the errors in the Error List window. This is because Visual Studio 2013’s real-time compiler hasn’t been replaced, and the development environment doesn’t understand the new language features. So this isn’t really a long term workable solution to develop in VS2013 with C# 6.0 language features. But you can make the code compile if you need to.