1

Using bool? variables, can somebody explain why true & null results in null and false & null results in false?

void Main()
{    
    bool? a = null;
    bool? b = true;
    bool? c = a & b;

    Console.WriteLine($"{b} & NULL => " + c);

    b = false;
    c = a & b;
    Console.WriteLine($"{b} & NULL => " + c);
}

Output:

True & NULL =>
False & NULL => False

I would also be happy about the duplicate, because I did not find it, yet.

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • A guess: False and whatever can never be be true, but true & whatever can be true or false. But it's just a guess. – Lasse V. Karlsen Jan 07 '19 at 12:03
  • This is not a bitwise and... You're not operating on integers – adjan Jan 07 '19 at 22:39
  • @Adrian thank you, "This is not a bitwise" I did not know how to specify the it is only one `&`. How to call it then ? – Mong Zhu Jan 08 '19 at 09:20
  • @MongZhu In C# we call `&` logical AND and `&&` conditional logical AND. Note that this only applies to `bool`. `&` is indeed the bitwise AND when applied to integers. – adjan Jan 08 '19 at 09:51
  • @Adrian cool, thanx for the detailed clarification and the edit. Cheers mate – Mong Zhu Jan 08 '19 at 10:29

4 Answers4

2

The purpose of null in this is case is to identity an unknown value. In three-valued logic, the purpose of the unknown value is to indicate that we do not know anything about the truthy or the falsy of a predicated.

false AND unknown returns false because it is not important to know what the second operator is. This is due to the and operator nature that requires both the operands to be true to return true. As we know that the first operand is false, the rest is irrelevant, and the result is false.

On the other hand, true AND unknown returns unknown, because the first operand is true, and therefore the result of the whole operation depends on the second operand. The second operand is unknown, therefore the result is unknown.

C# indicates unknown with null.

Yennefer
  • 5,704
  • 7
  • 31
  • 44
  • I mark this as accepted, because it has the most detailed explanation. – Mong Zhu Jan 07 '19 at 13:16
  • But it doesn't explain why `unknown & false` returns `false` and not `unknown`. – spodger Jan 07 '19 at 14:19
  • The AND operator returns true only when both of them are true. If at least one operand is false, the result is false. This is the definition of the AND operator. The fact that we are dealing with null here does not affect the outcome of this particular evaluation. – Yennefer Jan 07 '19 at 14:39
1

In this condition (a & b), compiler just when find 1 false , returns false, else compare a and b, in his condition cant return real result, then returns nothing!

CodeMan
  • 671
  • 6
  • 13
1

Using nullable types means your booleans can actually have 3 values, true, false and null. This means you have to thing in terms of three-valued logic.

This answer (which is for a sql question) covers the results of AND and OR when using three-valued logic.

Sean
  • 60,939
  • 11
  • 97
  • 136
1

The answer is that the bool? type has been designed to match a SQL Boolean, as described here Using nullable types (C# Programming Guide) and quoted below

The bool? type

The bool? nullable type can contain three different values: true, false, and null. The bool? type is like the Boolean variable type that is used in SQL. To ensure that the results produced by the & and | operators are consistent with the three-valued Boolean type in SQL, the following predefined operators are provided: •bool? operator &(bool? x, bool? y) •bool? operator |(bool? x, bool? y)

The semantics of these operators is defined by the following table:

x y x&y x|y

true true true true
true false false true
true null null true
false true false true
false false false false
false null false null
null true null true
null false false null
null null null null

Note that these two operators don't follow the rules described in the Operators section: the result of an operator evaluation can be non-null even if one of the operands is null.

Community
  • 1
  • 1
spodger
  • 1,668
  • 1
  • 12
  • 16