C# 6.0 – Some Nice Additions!

August 12, 2015 By 0 Comments

So, at some point recently, my Visual Studio 2015 preview updated to the full version. I’m not exactly sure when the release took place without looking it up, but now that it’s out, I’m hoping more people will use it. Particularly people I’m working with (be they colleagues or friends). The main reason is the host of new features in C# 6.0; ones that unfortunately don’t compile in Visual Studio 2013, but are really quite awesome.

Here are a few that I particularly like…


This is a very simple method that’s added in, that really helps if you refactor a lot, especially with the built in tools for it. It simply returns a string containing the name of a variable you pass it.

Why might this be useful? Consider this simplified example:

In this case, we decide that str isn’t really a very good name. Lets refactor it so it’s a bit better.

Now our exception makes no sense! Because it’s a string literal, our refactor didn’t update it for us. Instead, we should update it so we don’t need to worry about this.

Exception Filtering

This is one of my favourite features. This lets you catch an exception, but only if certain conditions are met.

Notice the when after the catch there? That defines the filter – any boolean expression will work. Personally, I like this for the purpose of keeping me from repeating my code. I’ll often have points where I want to handle different exceptions in different ways, and some will overlap and want to be handled the same way, while the others remain different. Some may not want to be caught at all. This tidies up the process and makes it much easier!

Auto Property Assignment

Being able to assign to an auto property at declaration without a backing field can help to reduce a lot of the noise of your code. You may be familiar with doing this:

Well, now that can be reduced to this:

Everything just got way shorter and much easier to read.

Expression Bodies

Expression bodies are an interesting new feature, intended to replace a get-only property’s getter, or a simple method’s body with a LINQ expression.

I’m a bit mixed about this one at the moment, but I can’t deny it is shorter than before!

Null Conditional Operator

Ever had to check if an object wasn’t null before calling a method on it? Perhaps a chain of nullable object properties? It might look something like this:

Well, this can be greatly shortened, and to as little as one line!

Notice the ?  just before the property there? That checks to see if someObject is null , and if so, the whole statement will return null (rather than throwing an exception). If someObject exists, then it’ll return the value of the property Length .

String Interpolation

This is the last new feature I’m going to cover, and is another way of formatting strings in a much more readable sense.

This is only a short example, but you’ll probably find that you’ve been doing this with a good many variables and placeholders. After a while, it can get confusing as to what all the numbers are. A better way would be this:

The $ at the start of the string signals that the string should be parsed for variables (denoted by {braces}) that should be used. Even in such a simple example, it should be clear just how readable this becomes, no?


There are more features added as well, and I highly recommend you go take a look at them. These are just a few I’ve come across that strike me as ones that will probably become rather common very quickly though. What do you think? Would you be likely to use these? Or any of the others?