C# – The ?? operator or coalesce operator

Here is a little code that returns either x or y. It returns y if x is null, otherwise it returns x.
return (x == null) ? y : x;

The coalesce operator is basically short hand for this. The ?? operator simplifies and shortens the above expression.
return x ?? y;

Leave a Reply

How to post code in comments?