简体   繁体   English

转换可空的布尔?布尔

[英]Convert nullable bool? to bool

How do you convert a nullable bool? 你怎么转换可空的bool? to bool in C#? bool C#中?

I have tried x.Value or x.HasValue ... 我试过x.Valuex.HasValue ......

You ultimately have to decide what the null bool will represent. 你最终必须决定null bool代表什么。 If null should be false , you can do this: 如果null应为false ,则可以执行以下操作:

bool newBool = x.HasValue ? x.Value : false;

Or: 要么:

bool newBool = x.HasValue && x.Value;

Or: 要么:

bool newBool = x ?? false;

You can use the null-coalescing operator : x ?? something 您可以使用null-coalescing运算符x ?? something x ?? something , where something is a boolean value that you want to use if x is null . x ?? something ,其中something是你想要使用的布尔值,如果xnull

Example: 例:

bool? myBool = null;
bool newBool = myBool ?? false;

newBool will be false. newBool将是假的。

You can use Nullable{T} GetValueOrDefault() method. 您可以使用Nullable{T} GetValueOrDefault()方法。 This will return false if null. 如果为null,则返回false。

 bool? nullableBool = null;

 bool actualBool = nullableBool.GetValueOrDefault();

The easiest way is to use the null coalescing operator: ?? 最简单的方法是使用null合并运算符: ??

bool? x = ...;
if (x ?? true) { 

}

The ?? ?? with nullable values works by examining the provided nullable expression. 可空值的工作原理是检查提供的可为空的表达式。 If the nullable expression has a value the it's value will be used else it will use the expression on the right of ?? 如果为空的表达式的值它的价值会被他人用来将使用权的表达??

If you're going to use the bool? 如果你要使用bool? in an if statement, I find the easiest thing to do is to compare against either true or false . if语句中,我发现最简单的方法是比较truefalse

bool? b = ...;

if (b == true) { Debug.WriteLine("true"; }
if (b == false) { Debug.WriteLine("false"; }
if (b != true) { Debug.WriteLine("false or null"; }
if (b != false) { Debug.WriteLine("true or null"; }

Of course, you can also compare against null as well. 当然,您也可以与null进行比较。

bool? b = ...;

if (b == null) { Debug.WriteLine("null"; }
if (b != null) { Debug.WriteLine("true or false"; }
if (b.HasValue) { Debug.WriteLine("true or false"; }
//HasValue and != null will ALWAYS return the same value, so use whatever you like.

If you're going to convert it to a bool to pass on to other parts of the application, then the Null Coalesce operator is what you want. 如果您要将其转换为bool以传递给应用程序的其他部分,那么Null Coalesce运算符就是您想要的。

bool? b = ...;
bool b2 = b ?? true; // null becomes true
b2 = b ?? false; // null becomes false

If you've already checked for null, and you just want the value, then access the Value property. 如果您已经检查了null,并且只想要该值,则访问Value属性。

bool? b = ...;
if(b == null)
    throw new ArgumentNullException();
else
    SomeFunc(b.Value);
bool? a = null;
bool b = Convert.toBoolean(a); 

This answer is for the use case when you simply want to test the bool? 这个答案适用于你只是想测试bool?的用例bool? in a condition. 在一个条件。 It can also be used to get a normal bool . 它也可以用于获得正常的bool It is an alternative I personnaly find easier to read than the coalescing operator ?? 这是一个替代我个人发现比coalescing operator ??更容易阅读coalescing operator ?? .

If you want to test a condition, you can use this 如果要测试条件,可以使用它

bool? nullableBool = someFunction();
if(nullableBool == true)
{
    //Do stuff
}

The above if will be true only if the bool? 以上如果只是bool?会是真的bool? is true. 是真的。

You can also use this to assign a regular bool from a bool? 您也可以使用这个分配规则的boolbool?

bool? nullableBool = someFunction();
bool regularBool = nullableBool == true;

witch is the same as 女巫是一样的

bool? nullableBool = someFunction();
bool regularBool = nullableBool ?? false;

Something like: 就像是:

if (bn.HasValue)
{
  b = bn.Value
}

The complete way would be: 完整的方式是:

bool b1;
bool? b2 = ???;
if (b2.HasValue)
   b1 = b2.Value;

Or you can test for specific values using 或者您可以使用测试特定值

bool b3 = (b2 == true); // b2 is true, not false or null

This is an interesting variation on the theme. 这是主题的有趣变化。 At first and second glances you would assume the true branch is taken. 在第一眼和第二眼,你会假设真正的分支被采取。 Not so! 不是这样!

bool? flag = null;
if (!flag ?? true)
{
    // false branch
}
else
{
    // true branch
}

The way to get what you want is to do this: 得到你想要的方法是这样做:

if (!(flag ?? true))
{
    // false branch
}
else
{
    // true branch
}

System.Convert对我很好。

using System; ... Bool fixed = Convert.ToBoolean(NullableBool);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM