A massive global ransomware attack has struck hospitals, companies and government offices around the world, seizing control of affected computers until the victims pay a ransom. ...
By: Chetan Vihite | September 10, 2015
What’s new in C# 6.0
In this article, I am going to talk about new language feature of C# 6.0. I am personally liking 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.
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’d 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 same as auto properties but when you create a read only auto property, c# compiler create a read only backing field and lets you assign this property in the constructor, much like how a read only field would work.
also, if you needed to initialize an auto-property to much like a variable then that’s now possible with c# 6.0. see below code-snippet on how this is done.
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 with simplifying this as a coding style so you are no longer needed to keep typing static class names through out 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.
observe that I have added an using statement along with static keyword and calling “WriteLine” method directly, instead of writing as “Console.WriteLine”
Another great feature of C# 6.0 is the string interpolation. ok, so let me ask you this first, how many times you have written string.format in your code? you would probably reply hundreds of time and that’s great, now another one, how many times you would 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 thing done.
lets see this in action:
note that how convenient its 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.
Another example would be
I am sure by now you must be thinking and appreciating how much these features will help you write clean and maintainable code.
to understand Index initializers, first check out the code below.
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 Mehtod, which is initializing a new JOnject 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.
not anymore you have to do this. because of Index Initializers your new and improved code would look like below ( a much simpler and clean ). isn’t it?
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 wont be a need to add several conditions explicitly to check nulls.
lets see how. but before that, lets take an example of converting a json to object.
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 code below. C# 6.0 introduces Elvis operator, “?.”, what “?” does is, it checks if the left part is not null, if its null then the whole expression is null, if its not null, then it goes further and executed the right part, this way it ensures there are no extra null checks are in the code and provides a better and clean way to write null conditions. cool, isn’t it? take a look at improved code below.
nameof operator is also 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.
returning a hardcoded string is a problem, so see below how nameof operator solves this,
Visual basic and F# both allows exception filtering, making a condition in your catch blocks and allowing you to filter it based on conditions. this was missing piece in C#, but with 6.0 its has matched what other .net languages offer.
take a look at code-snippet below.
the catch block will filter the exception based on provided condition.
Also, Catch and finally blocks “finally” have incorporated async processing
And that concludes my article on all the latest features of C# 6.0.
Link to sample source code
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.