不能隐式转换类型'bool?”“bool”。存在显式转换(您是否丢失了强制转换?)

时间:2021-10-02 16:35:29

Error : cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?)

错误:不能隐式转换类型'bool?”“bool”。存在显式转换(您是否丢失了强制转换?)

Code :

代码:

Test obj = new Test();
obj.IsDisplay = chkDisplay.IsChecked;

but when i use this means cast in bool then there is no error.

但是当我使用这个方法时,就不会有错误。

Test obj = new Test();
obj.IsDisplay = (bool) chkDisplay.IsChecked;

I need to know that, why need to cast this bool to bool.

我需要知道,为什么要把这个东西扔给bool。

Thanks

谢谢

7 个解决方案

#1


24  

You've declared IsChecked as a bool? (Nullable<bool>). A nullable boolean can be either true, false or null. Now ask yourself: If IsChecked was null, then what value should be assigned to IsDisplay (which can only take a true or false)? The answer is that there is no correct answer. An implicit cast here could only produce hidden trouble, which is why the designers decided to only allow an explicit conversion and not an implicit one.

你说过被检查为bool?(可空< bool >)。可空布尔值可以是真、假或空。现在问问自己:如果IsChecked是null,那么应该给IsDisplay赋什么值(只能取真或假)?答案是没有正确的答案。这里的隐式转换只会产生隐藏的麻烦,这就是为什么设计者决定只允许显式转换,而不允许隐式转换。

#2


31  

As the others stated bool? is not equal to bool. bool? can also be null, see Nullable<t> (msdn).

正如其他人所说的bool?不等于bool。bool吗?也可以为空,见Nullable (msdn)。

If you know what the null state wants to imply, you easily can use the ?? - null-coalescing operator (msdn) to convert your bool? to bool without any side effects (Exception).

如果你知道零状态的含义,你可以很容易地使用?- null-coalescing算符(msdn)来转换bool?无任何副作用(例外)。

Example:

例子:

//Let´s say "chkDisplay.IsChecked = null" has the same meaning as "chkDisplay.IsChecked = false" for you
//Let "check" be the value of "chkDisplay.IsChecked", unless "chkDisplay.IsChecked" is null, in which case "check = false"

bool check = chkDisplay.IsChecked ?? false;

#3


10  

bool? is not a bool. It is in reality a Nullable<bool> http://msdn.microsoft.com/en-us/library/b3h38hb0(v=vs.110).aspx

bool吗?不是一个布尔值。它实际上是一个可空的 http://msdn.microsoft.com/en-us/library/b3h38hb0(v=vs.110).aspx

If you need the bool value then you should either cast like you are doing or call the .Value property on the bool?. There is also a .HasValue property you can check to make sure that it is not null.

如果您需要bool值,那么您应该像您所做的那样强制转换,或者调用bool上的. value属性?您还可以检查. hasvalue属性以确保它不是null。

If IsChecked is null, this line will error.

如果IsChecked为null,这一行就会出错。

obj.IsDisplay = (bool) chkDisplay.IsChecked;

#4


4  

bool is not equals bool?

bool不等于bool?

bool can take two values, true and false

bool可以取两个值,真值和假值

bool? can take three, true false and null

bool吗?可以取三,真假零

That is why they are different

这就是他们不同的原因

#5


4  

I'm facing your question when I'm using the null check operator ?.:

当我使用零校验运算符时,我正面对你的问题。

if (!RolesList?.Any()) //Not accepted: cannot convert bool? to bool

So I'm using this instead

所以我用这个代替。

if (RolesList?.Any() != true)
  //value is null or false

In your case you should set it like so:

在你的情况下,你应该这样设置:

obj.IsDisplay = chkDisplay.IsChecked ?? false;

#6


2  

You can use below code

您可以使用以下代码。

obj.IsDisplay = chkDisplay.IsChecked == true?true:false;

#7


1  

chkDisplay.IsChecked is of type bool?. Which means it can hold values true, false and null. However, obj.IsDisplay is of type bool. Which means it can only hold true or false.

chkDisplay。ischeck是类型bool吗?这意味着它可以保存值true、false和null。然而,obj。IsDisplay是bool类型。这意味着它只能成立真或假。

Hence you have to explicitly cast it to type bool. However, this will still throw an exception if, the value you are trying to cast to bool is null.

因此必须显式地将其转换为bool类型。但是,如果您试图强制转换到bool的值为空,这仍然会抛出一个异常。

bool? nullableBool = null;
bool notNullableBool = (bool)nullableBool; //This will throw InvalidOperationException

#1


24  

You've declared IsChecked as a bool? (Nullable<bool>). A nullable boolean can be either true, false or null. Now ask yourself: If IsChecked was null, then what value should be assigned to IsDisplay (which can only take a true or false)? The answer is that there is no correct answer. An implicit cast here could only produce hidden trouble, which is why the designers decided to only allow an explicit conversion and not an implicit one.

你说过被检查为bool?(可空< bool >)。可空布尔值可以是真、假或空。现在问问自己:如果IsChecked是null,那么应该给IsDisplay赋什么值(只能取真或假)?答案是没有正确的答案。这里的隐式转换只会产生隐藏的麻烦,这就是为什么设计者决定只允许显式转换,而不允许隐式转换。

#2


31  

As the others stated bool? is not equal to bool. bool? can also be null, see Nullable<t> (msdn).

正如其他人所说的bool?不等于bool。bool吗?也可以为空,见Nullable (msdn)。

If you know what the null state wants to imply, you easily can use the ?? - null-coalescing operator (msdn) to convert your bool? to bool without any side effects (Exception).

如果你知道零状态的含义,你可以很容易地使用?- null-coalescing算符(msdn)来转换bool?无任何副作用(例外)。

Example:

例子:

//Let´s say "chkDisplay.IsChecked = null" has the same meaning as "chkDisplay.IsChecked = false" for you
//Let "check" be the value of "chkDisplay.IsChecked", unless "chkDisplay.IsChecked" is null, in which case "check = false"

bool check = chkDisplay.IsChecked ?? false;

#3


10  

bool? is not a bool. It is in reality a Nullable<bool> http://msdn.microsoft.com/en-us/library/b3h38hb0(v=vs.110).aspx

bool吗?不是一个布尔值。它实际上是一个可空的 http://msdn.microsoft.com/en-us/library/b3h38hb0(v=vs.110).aspx

If you need the bool value then you should either cast like you are doing or call the .Value property on the bool?. There is also a .HasValue property you can check to make sure that it is not null.

如果您需要bool值,那么您应该像您所做的那样强制转换,或者调用bool上的. value属性?您还可以检查. hasvalue属性以确保它不是null。

If IsChecked is null, this line will error.

如果IsChecked为null,这一行就会出错。

obj.IsDisplay = (bool) chkDisplay.IsChecked;

#4


4  

bool is not equals bool?

bool不等于bool?

bool can take two values, true and false

bool可以取两个值,真值和假值

bool? can take three, true false and null

bool吗?可以取三,真假零

That is why they are different

这就是他们不同的原因

#5


4  

I'm facing your question when I'm using the null check operator ?.:

当我使用零校验运算符时,我正面对你的问题。

if (!RolesList?.Any()) //Not accepted: cannot convert bool? to bool

So I'm using this instead

所以我用这个代替。

if (RolesList?.Any() != true)
  //value is null or false

In your case you should set it like so:

在你的情况下,你应该这样设置:

obj.IsDisplay = chkDisplay.IsChecked ?? false;

#6


2  

You can use below code

您可以使用以下代码。

obj.IsDisplay = chkDisplay.IsChecked == true?true:false;

#7


1  

chkDisplay.IsChecked is of type bool?. Which means it can hold values true, false and null. However, obj.IsDisplay is of type bool. Which means it can only hold true or false.

chkDisplay。ischeck是类型bool吗?这意味着它可以保存值true、false和null。然而,obj。IsDisplay是bool类型。这意味着它只能成立真或假。

Hence you have to explicitly cast it to type bool. However, this will still throw an exception if, the value you are trying to cast to bool is null.

因此必须显式地将其转换为bool类型。但是,如果您试图强制转换到bool的值为空,这仍然会抛出一个异常。

bool? nullableBool = null;
bool notNullableBool = (bool)nullableBool; //This will throw InvalidOperationException