views:

61

answers:

2

In C#, for example, when I compare two nullable booleans (bool?) I get the following results :

true & null = null
false & null = false
true | null = true
false | null = null

The issue is that I couldn't understand how those results come, what's the rule which I can use to determine the result of a logical operator on two booleans, when one of them is null?

+10  A: 

The idea is that "null" means "unknown" here, or "not enough information". So if the answer depends on the unknown value, the answer itself is unknown. If the answer will be the same whatever the unknown value is (e.g. true | null) then you're still okay.

Think of it this way:

y = true & x; // the result depends on x - it has to be true
y = true | x; // the result will be true whatever x is
Jon Skeet
Thanks, I understand that now!
Moayad Mardini
+2  A: 

Have a look at this link

http://msdn.microsoft.com/en-us/library/2cf62fcy.aspx

astander
Thanks, the link has helpful information.
Moayad Mardini