Logo

quadroid

Home
RĂ©sume
Tech Blog

Roslyn and C# 6.0 Language Features

Roslyn is the next big thing coming with Visual Studio 2015, and the awesome C# 6.0 language features got backed into the C# Compiler. Even though most of the features do not provide anything new, they help a lot with the easy things of the everyday work.

The full feature list is available on the codeplex site, I will show some uses cases that I cannot wait to get my hands on here:

Null propagation Operator

This is one of the biggest features of the new compiler, removing a ton of boilerplate if clauses:

One of the beautiful things you can do with this is writing thread save event invocations in one line:

pre 6.0:

var handler = this.TextChanged;
if(handler != null)
   handler(this, EventArgs.Empty);

with C# 6.0:

this.TextChanged?.Invoke(this, EventArgs.Empty);

There are hundreds of use cases for this, for example in combination with the null-coalescing operator:

Console.WriteLine(this.User?.Name ?? "Unknown User");

Following code will print "Unknown User" if the User instance is null or the User.Name is null.

Await in catch/finally

Well this feature is pretty forward, the new language feature of C#5.0 (async/await) is now usable in catch/finally blocks.

Getter-only auto-properties

pre C# 6.0 I wrote code to like this a lot:

public class AClass
{
   private readonly Something something;

   public AClass(Something something)
   {
      this.something= something;
   }

   public Something Something {get { return this.something; } }
}

with C# 6.0 it is possible to create getter only auto properties, those can be assigned right in place or from the constructor, above example can now be done without the boilerplate backing field.

public class AClass
{
   public AClass(Something something)
   {
      this.Something = something;
   }

   public Something Something { get; }
}

There are a ton more features that are worth a look, I can't wait for Visual Studio 2015 (the new blend is quite cool too!) Give it a shot and get the VS 2015 Preview!