I just read Shortest way to check for null and assign another value if not, but a question remains.
Consider this commonly seen code pattern:
if ( !string.IsNullOrEmpty(source) )
dest = source;
It's possible to express it as a one-liner using the null-coalescing operator:
dest = source ?? dest;
[Edit:] Thanks @Rango for realising these are not equivalent. But the following sure is:
dest = !string.IsNullOrEmpty(source) ? source : dest;
However, one would guess this alternative is not strictly equivalent to the former (although the observable result is). I say "guess" in this context because I'm not sure of whether the IL bytecode would be identical or not.
In either case, are there any shorter ways of not assigning anything, not even the original value, to the variable dest in the case where source is null? Even if a trick using a bitwise operator or something more clever. I was actually hoping for a syntax similar to the null-conditional operator.
Thanks.