问题描述
我想知道什么是对可空的bool做条件检查最干净的和可以理解的语法。
I was wondering what was the most clean and understandable syntax for doing condition checks on nullable bools.
在下面的好或坏的编码风格?有没有一种方法能恩preSS的条件更好/更干净?
Is the following good or bad coding style? Is there a way to express the condition better/more cleanly?
bool? nullableBool = true;
if (nullableBool ?? false) { ... }
else { ... }
特别是如果(nullableBool ??假)的一部分。我不喜欢如果(x.HasValue&安培;&安培; x.Value)
风格...
especially the if (nullableBool ?? false) part. I don't like the if (x.HasValue && x.Value)
style ...
的(不知道这个问题是否已经被问过......找不到与搜索类似的东西)的
推荐答案
我想很多人都专注于一个事实,即这个值是空的,而不会去想他们真正想要的东西:)
I think a lot of people concentrate on the fact that this value is nullable, and don't think about what they actually want :)
bool? nullableBool = true;
if (nullableBool == true) { ... } // true
else { ... } // false or null
或者,如果你想要更多的选择...
Or if you want more options...
bool? nullableBool = true;
if (nullableBool == true) { ... } // true
else if (nullableBool == false) { ... } // false
else { ... } // null
(nullableBool == true)而
将永远如果布尔返回true?为null:P
(nullableBool == true)
will never return true if the bool? is null :P
这篇关于最好的方法来检查可空布尔在条件前pression(如果...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!