Fragrance
|
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.
Rishi
Comments
|
Categories
All
Archives
May 2020
|