Why I'm excited about C# 6.0

Version 6.0 has been out since April via the new .NET compiler, Roslyn and will soon ship with Visual Studio 14 so let's have a super-quick run down of what's new. The majority of new features are syntactic sugar (isn't everything, really?) but in my opinion this is good thing and makes our lives much easier.

Remember that these are core language changes and independent of the .NET framework so you can start using C# 6.0 in your existing projects right now with the Roslyn compiler.

Null Propergation

I think this is the best feature since LINQ was introduced into .NET 3.5. Everyone knows the pain of checking if objects are null which can easily become messy:

if (person != null && person.Address != null)  
    person.Address.Street = "hello";

and let's face it, it's not pretty. With the new null propagating operator ?. we can turn the above into:

person?.Address?.Street = "hello";  

Now if person or Address is null no exception will be thrown otherwise Street will get set - neat! This also works on methods: GetPerson()?.Addresses?.FirstOrDefault()?.Street = "hello";

x?.Dispose() BAM! How sexy is that? x won't be disposed if it's null.

Exception Filtering

Another one to reduce "code noise" and update C# to include features from VB.NET and F#.

Consider a scenario where you want to only handle a unique constraint violation from PostgreSQL. Usually you would do something like this:

"Name" column has a unique constraint. "Paul" already exists so an exception will be throw.

   AddPersonToDatabase(new { Name = "Paul", Age = 25 });
catch (NpgsqlException ex)  
    if (ex.ErrorCode == 23505)
        // do something


But with exception filtering we can do this:

    AddPersonToDatabase(new { Name = "Paul", Age = 25 });
catch (NpgsqlException ex) if (ex.ErrorCode == 23505)  
    // do something

The catch block will only execute if the condition has been matched and looks a lot nicer in my opinion.

It also makes debugging easier as we don't have to rethrow the exception and mess up our stack. The debugger will show where the original exception came from, rather than where we rethrown from.

Primary Constructors and Auto Property Initializer

03/10/2014 - Primary Constructors have been removed from C# 6.0

This is a fantastic addition and very easy to understand:

public class Person(string name, int age)  
    public string Name { get; set; } = name;
    public int Age { get; } = age;

You can see your classes primary constructor can be defined along with the class name. Any properties can be set with any value you like just by assigning to them as you would a variable.

Declaration Expressions

03/10/2014 - Declaration Expressions have been removed from C# 6.0

Another one of my favourites. We all know this pain:

int id;  
if (!Int32.TryParse("1", out id))  
    // do something with id

But now we can declare out variables inline: if (!Int32.TryParse("1", out int id)) - super tasty.

Property and Method Expressions

This is also pretty sexy. Before:

public string FullName { get { return FirstName + " " + LastName; } }  

but with property expressions:

public string FullName => FirstName + " " + LastName;  

There's also index initializers for arrays, IEnumerable, etc, await in catch/finally blocks, the new $ sign and much more.

I love the direction Microsoft is going lately, they really are leading the way in the development world. The commitments they've made to open source community to improve the mono project is outstanding. Along with the C# 6.0, vNext, Rosyln and the new MVC framework I can't wait to see what else they come up with.