Exploring the Null Conditional Operator in C# 6.0
Continuing with the features of C# 6.0, we will now explain the concept of the null conditional operator.
Before we explain this feature, let's see why we need this feature. Assume we have a class with two properties.
We create an instance of this class but do not assign any value to the Token property. So it will be NULL. So in our code, when we use these properties, we add the NULL check for the Token property.
Look's good and works fine. With C# 6.0, this NULL check process is made more simplified. In order to check any value, we can simply use the convention.
InstanceName.?PropertyName
Confused? Let's replace the preceding convention with the codeinstanceName => userData and PropertyName => Token. So we change our code to C# 6.0 and check the NULL using the preceding convention. So our code becomes.
So what we did is, we simply added "?" to check the value of the Token and write it to the Console, if it is available else do nothing. Let's add the binary operator to print the message if no token is available. So we simply add the message as.
Run the code and see the results.
![NullOperator]()
Figure 1: Result
Easy to use, isn't it?