可能重复:AND operation cannot be applied between nullable bools.
我希望它的行为类似于+或*。因此,如果操作数中有任何一个为null,则应返回null,但是如果在&&或||中使用它,则编译器会发出警告。为什么?
举个例子:
举个例子:
//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)
....
&&
和||
运算符属于布尔类型,因此始终返回布尔结果。如果您使用可空类型,只需使用myNullable.HasValue && ...
。 - Dirk Vollmar