?? operator in C# is a binary operator. It is also called as null coalescing operator, which returns the left hand operand if it has any value, else, returns the right hand operator. Confused, let's see an example for this.
Suppose we have a property UserId of type int and nullable in nature. While using this property, if it has any value i.e. Not Null, then use that value, else, use some other value. So first thing that comes to mind is use the if-else, something like below:

Null coalescing operator provides a very shorthand syntax for this, which changes the code to:

Easy and concise to use. Happy coding...!!!
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.

Son Huy TranPosted Feb 17, 2015, 4:57 AM
In the conditional expression of the first example, I think the condition must be "if (UserId != null)", mustn't it?