Advanced C# Operators
I often find use for the C# ? operator but don’t use it often enough to have it memorized. While it isn’t the most readable operator, it certainly shrinks two lines of code into a very concise single line. Worth the lack of readability.
Then I discovered the ?? operator which is another great way to short hand.
if (variable == null) return null; else return variable;
It is a very convenient way to collapse that if-else statement into a single line. I thought I’d share as well as note them here for posterity’s sake.
| a ? b : c | if a is true, returns the value of b, otherwise c |
| a ?? b | if a is null, returns b, otherwise returns a |