What is going on CSharp 6

Hello!

I´m back again into the arena. Sorry for the long time since I last wrote a post, back in August. Certainly there have been lots of things to take care off, but I don’t want to stop sharing things with my friends. The good part is that I´ve been learning lots of interesting things I will share with you.

A couple of days ago an interesting Pluralsight course fell into my hands, What’s new in C# 6.0, by Scott K Allen. I really recommend if you can watch it, not only because of the content, but also because Scott is a great speaker and teacher. I felt really amazed with what is coming in C# world, so I wanted to post about the main new features of the language.

Everything I’ll tell is possible because of the new compiler, .Net Compiler Platform, Roslyn. It is an open source project that we can download and see the source code. It comes along with a new Visual Studio 2014. At the time of writing this, every of this is under beta testing version so I have downloaded it for free and try it.

Autoproperty initializers

With C#6.0 we can set the initial value of properties easily:

Image description

See how we can initialize even a read only property.

Primary Constructors

Primary constructor allows us to define a class constructor and capture the constructor parameters to initialize class properties.

In C# we have a common pattern, we inject a component through a constructor and we use this injected component throughout the rest of the class:

Image description

With Primary Constructor we don’t need to specify the constructor, instead we can use primary constructor together with autoproperty initializers:

Image description

The code becomes more elegant and concise and the benefit is that the menu primary contructor variable is available in the rest of the class as well.

Dictionary initializers

Initializing a dictionary is now more concise with the new syntax:

Image description

Event initializers inside constructors

Until CSharp 6 the following piece of code is illegal:

Image description

Params input parameters now with IEnumerable:

Now we can specify a variable number of arguments with IEnumerable and not only with array, like this:

Image description

Declaration expresions

Normally we declare variable and then we assign it a value of an expression. C# 6 will allow us to join the declaration and the expression. Let me show you an example:

Image description

See how we declare out total inside the TryParse.

The benefit is that the code has become more explicit and that we have declare the variable where it makes more sense, taking more control over the scope.

Using static

Simple but cool feature. If we have a static method on a static class, now we can declare the static class in the using part and just invoke the static method anywhere in the file. Therefore we don’t have to type the typical ``Assert.AreEqual()`, but instead:

Image description

Conditional access

Now instead of having lots of null checks before accessing properties of object, we can use ?. so we’ll get the value of the property only if the object is not null, like this:

Image description

Expression bodied members

This feature allows to assign values of properties using Lambda syntax, like we have done here for NumberOfDishes:

Image description

There are a few more of features and some of the explained here may be dropped, but this post is just a taste of what we will have soon available. It makes a bunch of small features but altogether it will allow to code simpler and more concise.

Remember, great power involves great responsibility.

Looking forward to have these tools in our daily projects!

Keep coding!

Written on October 4, 2014