little known, little used C# operators – null-coalescing operator (??) and the ternary operator (?:)

I was reading through some code over the weekend and came across a null-coalescing operator. So I thought I would write a blog post about it and the ternary operator.

 

null-coalescing operator (??)

x = y ?? z

If y is null, then z is assigned to x, otherwise y will be assigned to x

 

ternary operator (?:)

a = (b < c) ? b : c

if the condition (b < c)  is true b will be returned, if the condition is not true c will be returned. 

 

 

Remarks:
Personally, I might use the null-coalescing operator however I think in most instances I would stay away from both operators. Mainly from a code readability and maintainability perspective.