The ?? operator is also known as the null-coalescing operator. It returns the left side operand if the operand is not null else it returns the right side operand.
int? x = null;
int y = x ?? 99;
Console.WriteLine("The Value of 'Y' is:" + y);
string message = "Operator Test";
string resultMessage = message ?? "Original message is null";
Console.WriteLine("The value of result message is:" + resultMessage);
In the preceding example, we have an integer variable "x" that is a nullable type and has a null value so in the result variable "Y" the return value is 99. The same is in the second example. We could check whether the message was null and return an alternate value and result message return "Operator test because message variable has value.
Liked By
Write Answer
How to use Null Coalescing Operator (?) in C#
Join MindStick Community
You have need login or register for voting of answers or question.
Shiva Shukla
24-Jan-2016