How to use ?? null-coalescing operator in c# . I have never used ?? operator. And How we can assign default value when it is null using ?? Operator.
How to use null-coalescing operator?
2124
04-Jul-2017
Shadman Kudchikar
19-Jun-2019The ?? operator is called the null-coalescing operator. You can use it to provide a default value for nullable value types or for reference types. The operator returns the left value if it’s not null; otherwise, the right operand. Here is an example of using the operator.
In this case, the value of y is -1 because x is null. You can also nest the null-coalescing operator, below is an example,
Of course, you can achieve the same with an if statement but the null-coalescing operator can shorten your code and improve its readability.
You can learn more about Null Coalescing Operator C# here.
Sushant Mishra
06-Jul-2017The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise, it returns the right hand operand.