Firstly source?. checks to see if the object being passed in is null, if it is that entire portion (source?.ToString())
will return null and due to the ?.
operator the .ToString()
does not get evaluated. This operator is short hand and is equivalent to writing:
if(source != null) {
return source.ToString();
} else {
return null;
}
Next, the null coalescing operator ( ??
) kicks in and it will return string.Empty instead of just a null if either source or the return from .ToString()
was null.
If called with null
it will return string.Empty
If called on an object that has a .ToString()
method that returns null
, it will also return string.Empty
If called with an object that has a value to return from .ToString()
it will return that value.