AND 运算不能应用于可为 null 的布尔值
我正在两个可为空的布尔值 (bool?
) 之间应用 AND 运算 (&&
),但它给了我错误
运算符
&&
不能应用于bool?
和bool?
类型的操作数
我如何在包含两个的语句中应用 and 运算可为空的布尔值?
另外,如果我有一个对话框结果,例如
dialog.ShowDialog () == DialogResult.OK
如何将其转换为可为空的布尔值,因为我需要放置“&&”在 if 条件中使用 this 的运算符,其另一个操作数返回可为 null 的布尔值? 代码如下:
if (dialog.ShowDialog () == DialogResult.OK && CheckProjectPath(dialog.FileName, true))
此 if 条件中的第二个操作数是可为空的布尔值。
I am applying AND operation (&&
) between two nullable boolean (bool?
) but it is giving me error that
Operator
&&
cannot be applied to operands of typebool?
andbool?
How do i apply and operation in my statement that contains two nullable bools?
Also if i have a dialog result like
dialog.ShowDialog () == DialogResult.OK
How can I convert it to nullable bool as i need to put '&&' operator with this in an if condition whose other operand returns a nullable bool?
Here is code:
if (dialog.ShowDialog () == DialogResult.OK && CheckProjectPath(dialog.FileName, true))
the second operand in this if condition is a nullable bool.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,你想发生什么?这是非法的,因为如果第一个操作数为空,就会发生不好的事情。
x && 是什么意思? y
表示可空布尔值 x 和 y?那么,可为 null 的布尔值首先意味着什么?可为 null 的布尔值意味着以下三种情况之一:那么
x && 是什么意思? y 是什么意思?它的意思是“仅当条件 x 为真时才计算 y”,但如果 x 可为空,那么我们可能不知道 x 表示的条件是否为真。
例如,假设我们有:
如果 SalesForNovemberWereMoreThanAMillionBucks 为 false,则不要尝试存钱,并且我们知道银行里没有钱。如果属实,则尝试存钱;如果失败了,那么我们在苏黎世就没有钱了;如果成功,我们就会这么做。
现在假设并非所有销售人员都报告了 11 月份的销售数据。我们知道 11 月份的销售额是否超过 100 万美元吗?不,11 月已经过去了;11 月已经过去了。销售额要么超过一百万美元,要么不超过一百万美元,但现在我们不知道。正确答案不是“假”,正确答案不是“真”:正确答案是“我们不知道”:null。
那么如果第一个操作数返回 null 该怎么办?我们不知道销售额是否超过一百万美元,所以尝试存钱或不尝试存钱是正确的做法吗?您是否应该根据缺失的信息采取行动?
编译器没有能力为你决定这个问题。如果你不想在结果未知的情况下存钱,那么你必须这样说:
(SalesForNovemberWereMoreThanAMillionBucks() ?? false )
表示“如果结果为 null,则将其视为 false”。同样,如果你说:
并且x为真,y为空,那么你应该怎么做?你是说“只有当x和y都为真时才进行Frob。x为真,我们不知道y是否为真”。那么你到底该不该Frob呢? 你不知道。 编译器也不知道。如果你想说的是“Frob if x is true and y is true or null”,那么说:
或者,“frob if x is true and y为 true,但如果 y 为 null 则不然”,然后说:
现在,如果您不使用
&&
运算符进行短路评估,则 不要使用首先是&&
运算符。使用&
运算符;它总是评估双方,所以这里没有歧义。说x & 是完全合法的。 y
如果x
和y
是可为 null 的布尔值。当然,你仍然不能在if
中使用那个东西;这需要一个布尔值,而不是一个可为空的布尔值。但你可以说:布尔?结果=x& y;
其中 x 和 y 是可为 null 的布尔值。
Well, what do you want to happen? The reason this is illegal is because bad things happen if the first operand is null.
What does
x && y
mean for nullable Booleans x and y? Well, what does a nullable Boolean mean in the first place? A nullable Boolean means one of three things:So what does
x && y
mean? It means "evaluate y only if condition x is true", but if x is nullable then we might not know whether the condition represented by x is true or not.For example, suppose we have:
If SalesForNovemberWereMoreThanAMillionBucks is false, then don't try to deposit the money, and we know we have no money in the bank. If it is true, then try to deposit the money; if that fails, then we don't have money in Zurich; if it succeeds, we do.
Now suppose that not all the salespeople have reported their sales figures for November yet. Do we know whether sales for November were more than a million bucks? No. November is in the past; either the sales were, or they weren't more than a million bucks, but right now we don't know. The correct answer is not "false", the correct answer is not "true": the correct answer is "we don't know": null.
So what should this do if null is returned by the first operand? We don't know whether sales were more than a million bucks, so is the right thing to do to try to deposit the money, or to not try to deposit the money? Should you take action on the basis of missing information, or not?
The compiler has no ability to decide this question for you. If you want to not deposit the money if the result is unknown, then you have to say that:
(SalesForNovemberWereMoreThanAMillionBucks() ?? false)
means "if the result was null, treat it as though it were false".Similarly, if you say:
and x is true, and y is null, then what should you do? You are saying "Frob only if x and y are both true. x is true, and we don't know whether y is true or not". So should you Frob or not? You don't know. The compiler doesn't know either. If what you want to say is "Frob if x is true and y is either true or null" then say that:
Or, "frob if x is true and y is true, but not if y is null" then say that:
Now, if you are not using the
&&
operator for the purposes of short-circuiting evaluation then don't use the&&
operator in the first place. Use the&
operator; it always evaluates both sides, so there is no ambiguity here. It is perfectly legal to sayx & y
ifx
andy
are nullable bools. You still can't use that thing in anif
of course; that requires a bool, not a nullable bool. But you can say:bool? result = x & y;
where
x
andy
are nullable bools.您可以使用类似
“您必须选择自己的默认值”之类的内容。
您应该采用另一种方式:您无法对可为空的操作数执行操作,因此您必须尝试从
bool?
转换为bool
。运营商??在这里非常有用:关于 "null-coalescing Operator" ?? :
如果 a、b 和 c 均为
null
,则 d 变为-1
。如果其中任何一个不为空,则使用第一个值。You can use something like
You have to pick your own defaults.
You should go the other way: You can't do operations on a nullable operand so you have to try to convert from
bool?
tobool
. The operator ?? is very useful here:Regarding the "null-coalescing operator" ?? :
if a, b and c are all
null
then d becomes-1
. If anyone of them is not null, the first value is used.我只是猜你可以尝试一下
I'm just guessing you can try
没有为
null
定义&&
操作。因此,您必须确保两个布尔值都不可为 null,最简单的方法是调用可为 null 布尔值的GetValueOrDefault()
方法。The
&&
operation is not defined fornull
s. Therefore you have to make sure that both booleans are not nullable, and the easiest way to do that is to call theGetValueOrDefault()
method of the nullable boolean.如果 CheckProjectPath 函数的目的是验证路径,我看不出结果应该是
bool?
的任何原因If the purpose of the CheckProjectPath function is to validate a Path I cannot see any reason why the result should be
bool?