Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#

What’s New in C# 6.0? - Null-conditional Operators

Rate me:
Please Sign up or sign in to vote.
4.66/5 (15 votes)
7 Dec 2014CPOL3 min read 37.4K   13   5
Null conditional operators in C# 6.0

Image 1

C# 6.0 brought a set of most useful features with Visual Studio 2015 Preview and .NET 4.6. If you haven't yet explored the features, check out the embedded links within this post to learn what’s coming with it. Download the preview to get your hands dirty before you use it in production.

Today in this blog post, we will discuss about a new feature called “null-conditional operators”, which will actually improve your productivity. Let’s start with it.

Image 2

Don’t forget to read my previous posts in this series:

“Null-conditional operators” – a new feature introduced by Microsoft CSharp Team in C# 6.0 will definitely improve the productivity of the developers by reducing lines of code. Not only this, it may also reduce the possible number of bugs in code keeping the code clean.

Like nullable types, null-conditional operators can be used now. Just put a ‘?’ (question mark) after the instance before calling the property on top of it. You don’t have to write additional if statements to check for null now. For example, let’s see a simple if condition which we will then see with the null-conditional operator in C# 6.0:

C# 6.0 - Simple Condition (www.kunal-chowdhury.com)

In the above code snippet, you can see how the Null-conditional operator (“?.”) can be used to reduce the number of lines in code file and provide an easy access to check for Null and return the result. Here, it will return the address only if the instance of Employee object is NOT Null. Otherwise, it will just return a simple null value.

You can even add more if conditions to create a nested if statements. To begin with, let’s take a simple nested if statement which we will then convert to ternary operator and then reduce it more to leverage the C# 6.0 feature:

C# 6.0 - Nested Conditions (www.kunal-chowdhury.com)

In the above code snippet, you can see how C# 6.0 provides the way to check for Null and return you the actual value if it satisfies all the cases. And that’s all in a simple way. Reduction of lines, reduction of efforts… reduction of bugs and/or NullReferenceException (only if the developer utilizes it).

Now let’s see how it can be used to return a default value if the condition does not satisfy at all. In the below code snippet, you can see that “??” conditional operator can be used along with the null-conditional operator to return a value. In this case, if either of emp (employee object) or MemberOfGroups value is null, it will return –1:

C# 6.0 - Default Values in Conditions (www.kunal-chowdhury.com)

Isn’t it so simple and a very good feature in C# 6.0? How much did you like it? Please drop a line below and share your feedback with the other developers. We would be happy to hear your favorite C# 6.0 feature. If I haven’t already covered that yet, I will surely post it here.

Don’t forget to subscribe to my blog’s RSS feed and Email Newsletter to get new article updates directly delivered to your inbox. I am also available on Twitter, Facebook and Google+. Connect with me there to get regular updates on what I share with the community.

Reference: http://www.kunal-chowdhury.com
You may like to follow me on twitter @kunal2383 or may like the Facebook page of my blog http://www.facebook.com/blog.kunal.

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
PraiseThanks Pin
Rabbles15-Aug-17 6:11
Rabbles15-Aug-17 6:11 
PraiseSimple and nicely presented 5 Pin
Gayan Buddhika13-Jul-16 13:30
professionalGayan Buddhika13-Jul-16 13:30 
GeneralMy vote of 5 Pin
DrABELL10-Jan-15 15:51
DrABELL10-Jan-15 15:51 
QuestionCool Change Pin
Youzelin8-Dec-14 16:38
Youzelin8-Dec-14 16:38 
General! Pin
jonhy058-Dec-14 13:01
jonhy058-Dec-14 13:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.