2

I found this statement in a library I am using. It is supposed to check if the current node in the cluster is a leader. This is the statement: (!(cluster.Leader?.IsRemote ?? true))

Why not just use (cluster.Leader?.IsRemote)? (disregard the ! operator, I know how that works)

Are there any benefits to this approach?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Luk164
  • 657
  • 8
  • 22
  • 2
    Cause using former one will end up being `bool` and latter - `bool?`. And it depends on context what is more appropriate (also obviously you will have different values in case of `Leader` being `null`). Also latter one can't be directly used in statements requiring `bool` like `if`. – Guru Stron Dec 30 '20 at 11:23

4 Answers4

6

Let's build a truth table for

 (!(cluster.Leader?.IsRemote ?? true))

construction (note that we have three values to consider: true, false, null):

 (!(cluster.Leader?.IsRemote ?? true)) : Result 
 ----------------------------------------------
                                  true : false
                                 false :  true <- the only true we have
                                  null : false

So, if you are looking for a simplification you can put it as

 (cluster.Leader?.IsRemote == false)
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
5

It means that if cluster.Leader?.IsRemote is null then consider is as true ?? true

Effectively null ?? true equals true.

So if your leader is null, then the whole statement will return false). Without the ?? null your statement would not even compile, as !null means nothing.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
2

Consider the following code:

public class Foo
{
    public bool IsBar { get; set; }
}

var foo = new Foo();

Console.WriteLine(!(foo?.IsBar ?? true));

While IsBar is declared as a regular, non-nullable bool, the ? operator in foo?.IsBar makes that expression of type bool?, not bool.

You can't use nullable bools in boolean expressions, see also if condition with nullable, so you need to make the expression explicit: what should happen if foo, and therefore foo?.IsBar is null?

That's where ?? true comes in. Alternatives:

Console.WriteLine(!(foo?.IsBar).GetValueOrDefault(true));
Console.WriteLine(foo?.IsBar == false);
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
1

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).

Harish
  • 789
  • 1
  • 7
  • 21