C# 6 - What's good?

To get a full list of completed and planned features included with C# 6 / Rosyln take a look here

It's great to see with Rosyln being build out in the open we get transparency on the new C# 6 features, because it's open source, you can download the code and play with them right away.

There are a few features that I’d like to mention here, which I have some thoughts on.

Primary Constructors and Auto Properties

This feature is right up my street. It gives you the ability to create immutable types without all the verbose boilerplate that inevitably comes with creating immutable objects in C#.

class Person(string firstName, string surname)
{
	public string FirstName { get; } = firstName;
    public string Surname { get; } = surname;
}

Just so you can compare, below is the boilerplate, which is currently needed to achieve the same result.

class Person
{
	private string firstName;
	private string surname;
	
    public Person(string firstName, string surname)
	{
		this.firstName = firstName;
		this.surname = surname;
 	}
 	
    public string FirstName { get { return firstName; } }
	public string Surname { get { return surname; } }
}

I think using the new primary constructors is much better to look at. Hopefully it will promote immutability in types because they require less boilerplate to implement, win.

It's worth noting here that:

public string FirstName { get; } = firstName;

Is not the same as:

public string Firstname { get; private set; }

The former is essentially a true readonly property, where as the latter is not.
On creation the compiler is assigning the initial value to the property, however the compile does not call the property setter when it does this, which is worth remembering.

Using’s for statics

I haven't quite made my mind up whether I'm for; or against this feature.

using System.Console;	// a using for a static class

namespace App
{
    class Program
    {
        static void Main(string[] args)
        {
            // Instead of Console.WriteLine();
            // We can leave out Console.
            WriteLine("Hello World");
        }
    }
}

By directly referencing the static class in a using, we can leave it out when calling a method on that class. Simple enough, only I think it adds another place you have to look to know exactly what is going on within your code. If I were to use Console.WriteLine("Hello World") there is no confusion as to what is happening here. Where as with the using, could we possibly think that WriteLine() is a method on our class?
Even if it crosses my mind for a split second, it's failed for me. Not sure if it's worth the risk of confusion for the sake of missing off Console..

Null propagating operator

For me, the null propagating operator is a nice welcomed addition.
Checking for nulls is tedious; anything, which can help remove the cruft, is a good thing.

// Without operator (?.)
var customer = GetCustomer();
string name = (customer == null) ? null : customer.name;

// with operator (?.)
string name = GetCustomer()?.name;

What's nice about this is you can string them up too:

int? length = GetCustomer()?.Address?.Line1.Length;

Pretty quickly you will start to read this line as if the null checks were there, but without the verboseness.

This operator is found in other languages such as coffeescript, it's nice to see it come to C#.

Await in catch and finally

This is a win. To be honest I wasn't aware that you couldn’t do this currently.
A nice example of where this would be handy is for logging exceptions, now we can do it async within the catch block; should you wish.

try
{
	Something();
}
catch(Exception e)
{
	await logger.LogExceptionAsync(e);
}

Declaration within expressions

We can now declare variables inside an expression. So instead:

int x;
int.TryParse(s, out x);

Becomes:

int.TryParse(s, out int x);

I’m a big fan of these. Again, the purpose is to limit the amount of code we have to write and of course; read.


Overall, most of these new features have been optimizations and compiler fluff which should help make our code much more readable.