gpt4 book ai didi

c# - 为什么我必须明确询问可空 bool 变量是否为真?

转载 作者:行者123 更新时间:2023-11-30 19:32:40 25 4
gpt4 key购买 nike

bool? nullableVar;
if(nullableVar)

给我一​​个错误但是

if(nullableVar==true) 

评价不错。

我不确定我为什么会给出如果 bool 不可为 null 则评估正常的原因?

最佳答案

if 的条件表达式必须隐式转换为 bool . Nullable<bool> 不能转换为bool ,但是第二个表达式已经是bool类型了,所以没关系。查看 IL,我相信第二个表达式确实被编译为:

if (nullableVar.GetValueOrDefault())

关于c# - 为什么我必须明确询问可空 bool 变量是否为真?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4959626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com