Possible Duplicate:
AND operation cannot be applied between nullable bools.
I would expect similar behavior like + or *. So if any of the operand is null, it should return null, but the compile complains if it’s used in && or ||. Why?
To illustrate the point:
//this compiles
int? x = null;
int? y = null;
var z = x*y;
// this doesn't compile
bool? x = null;
bool? y = null;
if(x && y)
....
If it were to return
null, then you invalidate the boolean requirement.||&&Basically, it would allow for a value other than
bool(specifically,null) within anifstatement which would be undefined behavior sinceifstatements requireboolean expressions.Interestingly, this is one of the areas where Java and C# differ. Java requires boolean values within the
ifstatement as well, but they allow their version ofbool?(Nullable<bool>, or justBooleanin Java) to benull. This leads to somewhat unexpectedNullPointerExceptions at runtime, which C# is avoiding with this requirement.