Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

What’s New in C# 6.0

0.00/5 (No votes)
11 Sep 2015 1  
What's new in C# 6.0

Introduction

In this post, I am going to talk about new language feature of C# 6.0. I personally like these features a lot because they help in writing clean and understandable code.

With the release of Visual Studio 2015 comes the much awaited version 6 of C#. according to mads togersen (PM of C# language team), this version introduces no big concepts but instead ships many small features that helps you write better and clean code.

Getter-only Auto-Properties

I am sure everyone has seen and used several types of properties and of those, auto-properties are the short hand and time saver ones. I used auto-properties most of the times. However, there was one limitation with auto-properties in the sense there was no auto feature for read-only properties. If you wanted to get one, then you would have to type a read only backing field and serve the need. But with C# 6.0, you get the read only auto-properties.

It worked the same as auto properties, but when you create a read only auto property, C# compiler creates a read only backing field and lets you assign this property in the constructor, much like how a read only field would work.

image

Also, if you needed to initialize an auto-property to much like a variable, then that’s now possible with C# 6.0. See the below code-snippet on how this is done.

image

Using Static Classes

If you are dealing with static classes, and often calling static-class-name.method, then you no longer need to do that. C# 6.0 has come up with simplifying this as a coding style so you are no longer needed to keep typing static class names throughout your code. Instead, you can directly call static method and it will still function as expected. To use this coding convention, you should specify in using statement as using static class-name. Check the below code snippet to understand this better.

image

Observe that I have added a using statement along with static keyword and calling “WriteLine” method directly, instead of writing as “Console.WriteLine”.

String Interpolation

Another great feature of C# 6.0 is the string interpolation. OK, so let me ask you this first, how many times have you written string.format in your code? You would probably reply hundreds of times and that’s great, now another one, how many times would you have to count and make sure that supplied arguments are in order? And yes, that was some inconvenience to developers. Why do I have to specify arguments first and then values, that sometimes leads to confusion and inconvenience. C# has added a great feature in terms of string interpolation that you no longer need to specify arguments (0, 1, 2, 3..) and then values. you can put your values directly inside this string.format and get things done.

Let's see this in action:

image

Note how convenient it is compared to older version of string.format.

Expression Bodied Properties

Pretty much like writing a lambda expression, now properties have the facility to expose a lambda instead of a method definition. This is again to encourage clean and maintainable code.

image

Another example would be:

image

I am sure by now you must be thinking and appreciating how much these features will help you write clean and maintainable code.

Index Initializers

To understand Index initializers, first check out the code below:

image

There is a simple class Employee__OLDWAY – that I am referring to as old way of doing things, nothing great but look at the ToJson Method, which is initializing a new JObject and then filling in index initializers as firstName and lastName, and explicit need to create an object is required so that we can return it back.

You do not have to do this anymore. Because of Index Initializers your new and improved code would look like below (a much simpler and cleaner, isn’t it?).

image

Null-Conditional Operators

Checking a null condition looks like some in-built mechanism in your C# code, doesn’t it? However, there is now a cleaner approach to this. There won't be a need to add several conditions explicitly to check nulls.

Let's see how, but before that, let's take an example of converting a json to object.

image

If you look at the class above, the whole intent of FromJson method is to take a json object and convert it to Employee object, but the majority of code is typed to check the null conditions.

C# 6.0 gives a better way to get rid of these nuisances. Take a look at the code below. C# 6.0 introduces Elvis operator, “?.”, what “?” does is, it checks if the left part is not null, if it's null, then the whole expression is null, if it's not null, then it goes further and executed the right part. This way, it ensures there are no extra null checks in the code and provides a better and clean way to write null conditions. Cool, isn’t it? Take a look at the improved code below:

image

nameof operator

nameof operator is also a new addition to C# 6. Its usage is simple, it simply returns the name of a type. You would ask, why do we need it? Consider a scenario where you are having to return a name of a type and you hardcode the name and return. It works fine, but what if some new developer comes in and changes the name of the your type, the hardcoded string is now out of sync. To address this particular situation, C# 6 has introduced nameof operator, which you use in exactly these kind of scenarios to have peace of mind even when someone changes the name of your type, a correct name will always be returned wherever you need it.

For example:

image

returning a hardcoded string is a problem, so see below how nameof operator solves this.

image

Exception Filters

Visual Basic and F# both allow exception filtering, making a condition in your catch blocks and allowing you to filter it based on conditions. This was a missing piece in C#, but with 6.0, it has matched what other .NET languages offer.

Take a look at the code-snippet below:

image

The catch block will filter the exception based on provided condition.

Also, catch and finally blocks “finally” have incorporated async processing.

See below:

image

And that concludes my article on all the latest features of C# 6.0.

Final Thoughts

I hope you have enjoyed reading this article and learnt something new.

Go ahead and download VS 2015 and start exploring these features on your own.

Happy coding!

Filed under: CodeProject, Uncategorized

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here