C# 6.0 - A Time for Clean Code

  • 26 July 2016
    • Bespoke development

In case you missed it C# 6 that ships with Visual Studio 2015 comes with some delicious new syntactic sugar for developers. The improvements allow for very clean and concise code. However with great power comes great responsibility, so there are some things to consider before implementing these new features. Today we will take a look at some of the highlights.

The Safe Navigation Operator & ?? Operator

Null checks are something every developer runs into every day. Consider how you would normally check for nulls when dealing with complex objects. Then consider this simplification.

var x = obj?.child?.child?.child;  
if (x != null) // TODO

The safe navigation operator can even be used in conjuction with the ?? operator as follows.

var x = obj?.property ?? “literal string value”;

Now you may ask why this is not the default behaviour. The reason being that using this operator can lead to the design intention of the code being less obvious. Not only that but it can also be used to make bad code design look better, making the obvious code improvements less transparent. Here’s an interesting article with more info on the common misuses: http://enterprisecraftsmanship.com/2015/05/11/3-misused-of-operator-in-c-6/

C# String Interpolation

Next up we look at string interpolation, which makes working with strings a walk in the park. First let’s look at a bad example of working with strings.

var x = 1;
var y = "shoe";
var z = 1 + ", 2, buckle my " + y;
Output = “1, 2, buckle my shoe”

This example is bad because string concatenation is expensive in terms of memory and the code is less elegant than the alternatives below. The next example is a better way the above code can be written without using string interpolation.

var x = 1;
var y = "shoe";
var z = String.Format("{0}, 2, buckle my {1}", x, y);

Then finally let’s try doing the same functionality above using string interpolation.

var x = 1;
var y = "shoe";
var z = $"{x}, 2, buckle my {y}";

This is just a taste of what you can do with string interpolation. Advanced uses of it include inline string formating, ternary operators etc. See this article for more information: https://msdn.microsoft.com/en-us/library/dn961160.aspx

Further Reading

We only covered the tip of the iceberg here. There are many more new functions to try out. Here is a list of further reading we recommend.

  • New way to initialize Dictionary
  • Two new Operators
    • Null – Conditional Operators
    • Using nameof Operator
  • Handle Exception in a better way
    • Conditional Exception Handling – Exception Filters
    • Using await in a catch or finally block
  • Less Code Same Result
    • Expression – Bodied Methods
    • Simplify Static Member Access – Using Statement With Static Classes
  • Easy and Clean Property Initialization
    • Initialize Auto-Property
    • Getter Only Auto-Property

Read more at: https://abhijitjana.net/2014/12/11/10-new-features-of-c-6-0-that-you-should-know/