Code Less in C#6

When a new version of any technology comes out they usually boast something that would make that version stand out. In C#6, the main focus was on the small features and improvements in order for C# developers to write better and cleaner code. Let’s find out how many new features are in this sample code:

If you’re wondering if that program compiles, yes, it does! It may look strange because of some of the “little changes” in C#6, but it would not be surprising if you start to use these features regularly once you’ve learned about them. Now let’s go deeper in the sample code.

In C#6, it is now possible to use the static keyword on imports. What this enables us to do is to directly call on a static member of the namespace without the need to include the prefix (i.e. the class name).

If you noticed that in that line, normally we write like this:

In C#6, we can omit including the name of the class (i.e Console) and it would still compile.

Looking at the content of the WriteLine above the string that it will write looks different as well because another feature that C#6 introduced is string interpolation. What it does is it allows us to format the string without using the string.Format(“{0},{1},{2}”, firstName, lastName, gender) method anymore.

This is one feature that would be used a lot because of the impact that it has. By using the string.Format() approach, we are prone to commit mistakes when we assign the values with the correct order and the line of code becomes less readable as well as it expands. With string interpolation, it will boost the readability and it will have a positive effect on the maintainability of the code as well.

The last strange thing in the WriteLine syntax in the sample above is the ?. symbol as shown in anotherDeveloper?.Name. That is the null operator (a.k.a. the Elvis operator: because the symbol looks like it has two dots for the eyes and the swirl of the question mark looks like Elvis’ hair). Anyway, the null operator is another great way for us to trim down some lines of code. Let’s look at a bigger piece of code as an example:

The null conditional operator is a great feature as it would trim down a bunch of lines of codes whenever it can be applied. This is one of my favorites in the new features of C#6.

Note: Make sure that the data type of the variable that you will use is nullable or else it would result to a compiler error.

The next feature is being able to set a default value for an auto-property. Let’s have a look in the example:

Lastly, we have the expression bodied methods feature that can serve as an alternative way on how we write single line methods. Here’s an example on how I applied it:

What using static on imports, string interpolation, null conditional operator, setting default values to auto-properties and expression bodied methods all have in common is that it enables us to do the same things with less code. Does having less code automatically means that we would have cleaner codes?

That becomes subjective and every developer is entitled to have an opinion whether to write it the new way as shown in this post or to stick in the old way to do it. Just like in the expression bodied method example where even if it looks cleaner, at first sight, but it is more difficult to understand than the old way we do it.

To get the most out of these features, we need to be consistent if we choose to go with it. Make it a standard in the project, inform and make sure every developer writes it the same way. Nonetheless, these new features could really become the norm soon. There are still a bunch of cool new features of C#6 like the exception filters.

Would you apply these new features in your code base? Which feature did you like the most? Feel free to discuss it below.