Improve Your Code by Using the New C# 6 Features

The new versions, C# 6 and Visual Studio 2015 come with some pretty cool new features. Mads Torgersen and Dustin Campbell gave a presentation at //build/ 2015 covering a lot of them, which can be viewed at https://channel9.msdn.com/Events/Build/2015/3-711. I want to point out and give a few samples of the ones I found the most interesting and useful.

IDE Features

Many new features have been added in Visual Studio 2015. They are mostly subtle, but they each make life just a little bit easier.

Fade Unused Code

Now, for example, in your list of using statements at the top of each file the unused ones become faded to tell you that your code is not actually using anything from those namespaces. As you can see below the only using statement being referenced is the System namespace. All the rest are not used in this particular coding file and are faded out.

Faded Usings Feature

Quick Actions

As you might have noticed there is also a new little light bulb to the left of the code. This little guy means there are suggestions to make your code better. Clicking on it shows you something like the screenshot below. It is showing you that it can remove unused using statements and is indicated in the red highlighted area. These new suggestions are designed to keep you in the coding context without a jarring dialog that takes over your focus.

Visual Studio Light bulb Feature

Refactoring

Another cool enhancement is the rename functionality. As you type a new name the rename functionality keeps you in the code editor and shows you the changes live as you type.

Visual Studio Rename Feature

Naming conflicts are auto-resolved, if possible, by adding things like the this keyword in-front of the conflict.
If you create an inline temporary variable and then use it, lets say in the next line, Visual Studio can easily get rid of that variable and combine the two lines. This helps make your code shorter, easier to read and maintain.

C# 6 Language Features

There are also many cool little additions to the language that make code more readable and faster to create due to it’s compactness and brevity.

Import using static

By adding in the using declarations at the top of your code file, where your using statements are, you can add something like “using static System.Console;” This will allow you to write the below code.

Static Using Statement Feature

Here you can see that instead of prefixing your method call with the static class name, you can simply call the static method. Personally I am not sure if this makes the code more readable or not. The static keyword Console, in this case is telling you more about where that line is being written. However using it on something like Math.Abs(5.5) -> Abs(5.5) I would say is more readable and clearer. This ability can be used with Enums as well. I would advise caution when using this new feature and only use it when it actually makes the code more clear and not more confusing. I believe this will depend on how telling names of the class and method are.

Method Declarations

Often we have simple method declarations that are one line and need {‘s and return keywords, in other words, lots of boilerplate code. Now we can get rid of that boilerplate and rewrite the old code in one line using lambda style notation like below.

New Method Declaration Feature

Interpolated Strings and nameof()

This is my favorite new feature. It makes writing log messages and things like throwing exceptions on guard clauses so much easier. Interpolated Strings remove the old string.Format() and replace it with $”” and the place holders like {0} are replaced with {variable} and are not needed as the last parameters any more.

Interpolated Strings Feature

Here I combined the use of the new nameof() method to get the name of the property instead of using a hard coded string in the old way example. This becomes really useful when throwing ArgumentNullExceptions and you want to reference the parameter name. The old way would be a string and not get updated if the parameter name was refactored, now it is updated automatically.

Null Conditional

Perhaps the single most useful new feature is the Null Conditional. This helps remove all the null checking spaghetti code. The “Elvis” operator, “?.”, can now be leveraged to check nulls inline and check properties as you reference them.

Null Conditional Feature

As you see above, If I wanted to get the 3rd record from a list of people, I would have to check that the list is not null, that the list has an element at 3 and that the third element is not null. Instead of doing a bunch of comparisons count checking it can be simplified using the Elvis operator as show above. The way this new operator works is by returning the value if not null and a null value if any in the chain are null.

Conclusion

Using all these new features will help strip out and compact code. It helps eliminate boilerplate code, increase readability, maintainability, and contribute to the all around cleanness of the code. As I think Mads sums it up the best:

“Room for more logic on your screen.” -Mads

A full list of the features Mads covers in the talk can be found here http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.aspx (note the notes at the end of his post for changes). An interesting list of proposed changes for C# 7 (future) can be found on GitHub https://github.com/dotnet/roslyn/issues/2136 and is worth a glance.

Let me know in the comments or on twitter @ScottKerlagon how you think these new features will help improve your code!

Tagged . Bookmark the permalink.