铸造可空布尔为bool [英] Cast nullable bool to bool
问题描述
我有一个对象AlternateName.IsMaidenName
我要强制转换成一个复选框 - IsMaidenName
它不会让我投的,因为它说无法将源类型可空目标bool类型。
我已经在我们的应用程序内的其他景点这个问题,但我想在那里把它扔出去一个更好的方式来处理这些问题。
IsMaidenNameChecked = AlternateName.IsMaidenName;
这是很合乎逻辑,你不能施放一个可空布尔?为BOOL,因为,如果布尔有什么价值,如果可空包含空
我会怎么做,是这样的:
IsMaidenNameChecked = AlternateName.IsMaidenName?假;
I have an object AlternateName.IsMaidenName
I want to cast that to a checkbox - IsMaidenName
It wont let me cast that as it says Cannot convert source type nullable to target type bool.
I've had this issue in other spots within our application but I wanted to throw it out there for a better way to handle these issues.
IsMaidenNameChecked = AlternateName.IsMaidenName;
It is quite logical that you cannot cast a nullable bool to a bool, since, what value should the bool have, when the nullable contains null ?
What I would do, is this:
IsMaidenNameChecked = AlternateName.IsMaidenName ?? false;
这篇关于铸造可空布尔为bool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!