Today we are going to explore yet another new feature of C# 6.0 which will be coming in this release. This is my fourth post in the series of C# 6.0 New Features. Don't forget to read my previous post in this Series :
Today we will be learning about String Interpolation/String Concatenation with respect to C# 6.0.
String Concatenation - It's used to join the different records to the users and to accomplish this we either use "+" symbol or string.Format method to format and to organize it. With the new release of C# 6.0, it's now a old mechanism.
But now with C# 6.0, Developer can directly put expressions in the string literal to show the values in proper format and you can even align or specify different format to your values. And one of the best is, Developer can write conditions within string literals with additional strings as content.
Below are the few examples :
Hope you like this feature and since Microsoft is yet to release the stable version of Visual Studio 2015 and C# 6.0, so you can expect quite a lot more feature in String Interpolation. Stay tune to my series of post on C# 6.0 Features. Happy Coding.
C# 6.0 includes a set of useful features with Visual Studio 2015 Preview and .NET 4.6. In my previous post I had explored "nameof" expression and how to assign the default values to the Auto-Properties.
Today in this post, we will discuss about a new feature called "Null Conditional Operators" that is really very handy, will improve your productivity, and may also reduce the potential bugs.
Developer can now use the null-conditional operator same way they use the null-types. Developer just have to include "?" after the instance and before calling the property on top of it. He doesn't have to include the null validation code. Below is the example.
From the above code you can see how null conditional operator reduces the no. of lines in the code and provides an easy and clean way to check for the NULL validation and even reduces the potential bugs. In the above "WardName" will be returned only if the Instance of "Bed" is not null, else it will return a simple null value.
Developer can even add more if condition to create a nested if statements. Below is the example.
From the above code snippet you can see how Null Conditional operator validates for the NULL and returns the value. And you can actually see how much less code Developer needs to write and with less potential bugs.
Now we will see how we can return the Default value if none of the condition is satisfied. Here we can use "??" operator to assign the default value along with Null Conditional operator. Below is the example.
With all the above example of Null Conditional Operator you can see how useful this feature is for Developer. Please share your feedback if any and also share this post with your fellow Developers. Happy Coding.