布尔?不可为空
Bool? not nullable
我的 class 中有以下字段:
public bool? EitherObject1OrObject2Exists => ((Object1 != null || Object2 != null) && !(Object1 != null && Object2 != null)) ? true : null;
但是在 Visual Studio 中,我收到一个 IntelliSense 错误,指出 "bool" 和 "NULL" 之间不能进行转换,即使该字段可以为 null。
我做错了什么?
是否有更简洁的方法来检查两个对象中的任何一个是否不为空,但其中一个必须为空?
尝试
? (bool?) true : null;
问题是默认 bool
(true) 不可为空,因此就编译器而言,case 语句返回不同的类型
并且您可以删除 Servy 指出的冗余
Object1 != null ^ Object2 != null
我的 class 中有以下字段:
public bool? EitherObject1OrObject2Exists => ((Object1 != null || Object2 != null) && !(Object1 != null && Object2 != null)) ? true : null;
但是在 Visual Studio 中,我收到一个 IntelliSense 错误,指出 "bool" 和 "NULL" 之间不能进行转换,即使该字段可以为 null。 我做错了什么? 是否有更简洁的方法来检查两个对象中的任何一个是否不为空,但其中一个必须为空?
尝试
? (bool?) true : null;
问题是默认 bool
(true) 不可为空,因此就编译器而言,case 语句返回不同的类型
并且您可以删除 Servy 指出的冗余
Object1 != null ^ Object2 != null