C# ? operator Null SQL – 2017

c# ? operator

Operator (C# Reference) The ?? 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.

?? Operator (C# Reference) – MSDN – Microsoft


Simple Example Usages   

Several folks have blogged about the ?? operator in the past – read here, here, here, and here for some previous examples on how to use it.  Simply put, the ?? operator checks whether the value provided on the left side of the expression is null, and if so it returns an alternate value indicated by the right side of the expression.  If the value provided on the left side of the expression isn’t null, then it returns the original value.

For example, let’s assume we have a string variable “message”.  We could check whether message was null, and return an alternate value using the code below:

Because the “message” variable above wasn’t null, the “result” variable is assigned the original “hello world” message value.


Related posts:

Leave a Reply