Let's consider the following code
class Program
{
static void Main(string[] args)
{
Cluster cluster = new Cluster { Leader = null };
var libresult = (!(cluster.Leader?.IsRemote ?? true));
var result = (!(cluster.Leader?.IsRemote));
}
}
class Cluster
{
public Leader Leader { get; set; }
}
class Leader
{
public bool IsRemote { get; set; }
}
As you can see Leader is null in above code
if consider condition as
var result = (!(cluster.Leader?.IsRemote));
then
Null-conditional/Elvis operator(?.)
return null for cluster.Leader?.IsRemote
so, !(null) will be null.
but if consider condition as
var libresult = (!(cluster.Leader?.IsRemote ?? true));
then as we have see above Null-conditional/Elvis operator(?.) return null for cluster.Leader?.IsRemote
so, now condition is !(null ?? true)
According to
Null-collation(??) operator
(null ?? true)
will return true. and !(true) will be false
so, benefit of "?? true" is that if left hand side value is null
then it should be consider as Boolean(true).