Warp logo logo on transparent background.

C# 6.0 – A Time for Clean Code

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/

Related Blogs

high five hands celebrating

Warp Development Recognised as a Clutch Global Honoree and Champion for Fall 2024

We are so excited to announce that Warp Development has been recognised as a 2024 Fall Clutch Global Award honoree and Clutch Champion.
cloud infrastructure and e-commerce

How to Scale Your Cloud Infrastructure for Black Friday 2024 Success 

Prepare your cloud infrastructure for Black Friday 2024 success with a comprehensive strategy.
Artificial Intelligence visual on a clear screen

AI in Software Development: Enhancing Efficiency Without Sacrificing Expertise

Discover how AI is transforming software development by enhancing efficiency and innovation without replacing human expertise.