What’s new in C# 6.0? - Null-conditional operators

C# 6.0 brought a set of most useful features with Visual Studio 2015 Preview and .NET 4.6. If you didn’t yet explore 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 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.



Don’t forget to read my previous posts on 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 no. 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 no. 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 cover that yet, will surely post it here.


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


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter , Facebook , Google+ and LinkedIn . I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.


  1. Awesome information... Thanks for sharing it...

  2. all new features introduced in http://www.kunal-chowdhury.com/ ; against their simplicity, they are extremely useful and helpful. writing an if statement to check if an expression is null or not, was really ornery. Also passing a string parameter was really a bug prone, and string concatenation using old ways was a bit time consuming and hard to read(in case of string.format). I think VS 2015, specifically C# 6.0 is going to be the most desirable one for .net developers

  3. Wow.. this is great. Thanks Kunal for sharing :)

  4. Not 100% equals for the second example:
    If Count property of emp.MemberOfGroups is int?, then if the value of this property is null, then in the old way, (emp != null && emp.MemberOfGroups.Count != null) ? emp.MemberOfGroup.Count : -1, could be null. It means, in old way, the result could be emp.MemberOfGroup.Count (but this could be null or non null value, it depends on the value of this property) or -1.
    But for the new way, the result will be emp.MemberOfGroup.Count (only could be non null value) or -1.

    1. Of course that would be a very poorly designed "Count" property - by convention a count would never be null since that's a meaningless condition

    2. Dear simple is that, here -1 will be put into the variable if the condition is null.... and -1 is also an integer ... Got it?

    3. wont this:

      (emp != null && emp.MemberOfGroups.Count != null) ? emp.MemberOfGroup.Count : -1

      always return an int? struct with it's Value property set to Count (if it is not null), -1, OR HasValue = False (if Count is null)

  5. The code looks clean and understandable. Nice feature!

  6. This is a golden nugget of coding. I got goosey skin of it! That's why I love C# so much - it gets only better. And quick!

  7. simple and direct information
    tks a lot

  8. One thing Kunal ! If there we wanna use OR Operator instead of && Operator then what will be the format of the Statement??? PLZ

    1. Hi Farhad,

      If you say "if(emp != null || emp.Address != null)" => possibility of NullReferenceException.
      Did I answer your question?

  9. Chowdhury ji... chhaaa gae aap toh

  10. It is up there at the top of the list of my favourite C# 6.0 features! Nice article, Kunal. I definitely will be reading the others in the series.

  11. This comment has been removed by a blog administrator.

  12. This is very interesting. It is a 'functional'-style approach looking similar to flatMap in Scala or bind in Haskell operating on Option types. I like the direction this is heading.


© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top