New C# 6.0 features are great evolution

Martin Pernica
4 min readMay 11, 2015

--

At Microsoft BUILD 2015 conference was announced a lot of cool stuff like iOS and Android apps on Windows Phone or the new code editor called Visual Studio Code.

But there is more, Microsoft released RC version of newest Visual Studio 2015 with new version of C# — 6.0.

Maybe it can look like big update, the major version number increased, but new features are rather evolution than big revolution. Is this fail? Absolutely not! Every software needs spare some major versions to enhance and polish already implemented features, add some syntax sugar and increase performance.

So why I bother with writing blog post about C# 6.0? Easy answer — these evolution features are pretty cool!

Using “static”

You know the use-case when you are using some helper class with static methods and you need every time write the name of class? Everybody knows this and it’s annoying. But with C# 6.0 you can use static keyword when you are importing the class into your scope and after that you can call static methods of that class without need to type the whole class name.

For example if your application heavily rely on system console output, you can now omit the Console class name.

Null conditional operator

One of the new features which I’m most excited about is the new null conditional operator.

You know the story about billion dollar mistake — the null references right? So you know it’s good to test your variables against null, because sometimes you can have null in your variable.

And sometimes it may be really boring to write — if object is not equal null, than call method and test result of method to null as well… boring. So C# 6.0 comes with cure — null conditional operator which will helps you write more readable code when you are testing value against null.

So how the new operator looks like? Just simple question mark (as you may know from other languages)!

Really cool!

Expression bodied functions

Sometimes your method doesn't do anything special, just return result or formatted value, like ToString method.

In C# 6.0 you can write these short method on one line with syntax similarly like lambda expression declaration, just short arrow.

Automatic property initialization

Many properties are initialized one time in constructor and also it can be boring if you have many of these properties. Using C# 6.0 you can rescue your constructors from this initialization and put it where it belongs — right to property declaration using simple equal sign.

Now if you look into example code you might think — hey man, you forget setter for Guid property! No I don't. Because this is next new feature.

This syntax without set; will create readonly backing field for Guid property with auto initialized value. Very useful and it can save many keystrokes for you.

String interpolation

Do you remember the expression bodied function example with ToString method? You think the method declaration can not be simpler? You are wrong! C# 6.0 introducing new feature called string interpolation (you may know this from other languages) using dollar sign you can easily create formatted string and write values directly into “holes”.

And also inside these “holes” you can write small logic, for example ternary operator.

Exception filters

You know the embarrassing feeling when comes to you your colegue which use VB.NET and shows you his code with nicely sanitized exceptions using filters (catch when stuff), right?

Now is time to not hide your exceptions catch code! Because C# 6.0 introducing exception filters.

And know what is cool as well? Now you can use await keyword inside your catch block!

Yes, this is future

Index initializers

From C# 3.0 we have object initializers which are really useful and used in many source codes. But what about index setters?

There is no way how to shorten your syntax if you use index setters. But this is not true anymore. C# 6.0 brings syntax for object initialization which have index setters in the way like we already have for classic object initializers.

The nameof operator

When you are throwing exception is good to describe in message what went wrong or which argument is not valid and so on. You can get class name from object, but what if you want directly write the name of argument into string? For example “Argument name contains invalid characters”. Yes you can hardcode the name into message string, but when you do some refactoring, mostly you will forgot rename the variable inside of these strings. C# 6.0 add the new nameof operator.

And we are at the end of post, as you can see many of these features are syntax sugar and helps you to be more productive, which is good!

So don't wait and download new Visual Studio 2015 and start experimenting with C# 6.0!

--

--

Martin Pernica

🕹 Games developer 📸 Passionate photographer ☕️ Coffee lover