In an API we're using, methods that return a domain object might return a special "null object". In the implementation of this, the comparison operator and the Equals()
method are overridden to return true
if it is compared with null
.
So a user of this API might have some code like this:
return test != null ? test : GetDefault();
or perhaps a bit more verbose, like this:
if (test == null)
return GetDefault();
return test;
where GetDefault()
is a method returning some default value that we want to use instead of null
. The surprise hit me when I was using ReSharper and following it's recommendation to rewrite either of this to the following:
return test ?? GetDefault();
If the test object is a null object returned from the API instead of a proper null
, the behavior of the code has now changed, as the null coalescing operator actually checks for null
, not running operator=
or Equals()
.