The 7th version of C# is coming, and it’s expected to bring some new and exciting features to our tool sets. Here’s the thing, though: Are you up to speed with its predecessor’s features?

A few days ago I was trying to figure out what my next post would be about. It seemed only natural to write about the features of C# 7 since we’re one day(!!!) from the release of Visual Studio 2017.

But then I realized that, even though it’s been almost two years since the release of C# 6, some developers still aren’t comfortable with its features, and have not incorporated them into their code.

So, I’ve figured I’d take a step back and write about some C# 6 features instead. Not all of them, but those that can make your code cleaner and more expressive and are simple enough for you to use right away.

String Interpolation

Suppose we have a Person class, having Name and Age as read-only properties. Something like this:

Then we have to add a method called Introduce() , to make our Person objects able to introduce themselves. For a person named “Mary” and aged 22, the method should return: “Hi! My name is Mary and I’m 22 years old.”

There are a few ways we could do that in C# 5, as you can see in the next listing:

Each one of them has their advantages and disadvantages, of course, but C# 6 offers a new way:

You put a dollar sign at the beginning of the string, place your variables inside curly braces - and that’s it!

String interpolation doesn’t work for all occasions. If you need some special formatting on the values you still have to use string.Format , for instance.

But on a day-to-day basis, I use interpolation most of the time when I need to concatenate strings. I find it way nicer and cleaner than the other options.

Auto-Property Initializer

Continuing with our previous example. Let’s say that we need to provide a parameterless constructor in our class. Along with that, we need to provide default values for Name and Age .

Otherwise, Introduce would return “Hi! My name is and I’m 0 years old.” And we wouldn’t want that.

In C# 5, we’d have to initialize the properties inside the constructor:

C# 6 let us provide default values for our properties when we declare them:

Null Conditional Operator

Consider the following code:

Nothing remarkable about it. But there’s a problem waiting to happen here. If customer is null , then this code will throw a NullReferenceException .

This is when the new “Null Conditional Operator” can come in handy:

How does that work? Simple: if customer is not null , the Address property is accessed, and its value is assigned to our local variable. However, if customer is null , then the whole expression evaluates to null . And that’s what is assigned to the variable.

Another nice thing about the Null Conditional Operator is that you can use it along with the null-coalescing operator to easily provide a default value, instead of null :

Expression-bodied methods and properties

This one is super easy. It’s just a shorter way of writing methods, using the syntax of lambda expressions.

For instance, the two methods do the same thing:

This feature isn’t restricted to methods, though. It can also be used with properties (only getters) and indexers.

Conclusion

It seems clear that Microsoft’s goal with C# 6 was not to add some radical new capabilities to the language.

Rather, they had the intention to enable developers to write code in a cleaner and more concise way. And in my opinion, they succeeded.

You may think that some of the features don’t make that much of a difference, in practice. I beg to differ. C# is often accused of being too verbose. There are situations in which you have to write a decent amount of code to accomplish something that a Ruby programmer or a Python programmer could solve with a one-liner.

And that’s fair enough. Some (most?) of said verbosity is due to C# being a statically typed language, and let me tell you: I’m glad for that. I can’t count how many times the compiler saved me by catching my dumb mistakes. If a little bit of verbosity is the price I have to pay for this, so be it.

That being said, the detractors have a point. It is possible to be too verbose. So, any feature that helps me eliminate some of that verbosity and write more concise code is welcome.

See more

Please enable JavaScript to view the comments powered by Disqus.