B. *_*non 14 c# boolean nullable
使用此代码:
private bool AtLeastOnePlatypusChecked()
{
return ((ckbx1.IsChecked) ||
(ckbx2.IsChecked) ||
(ckbx3.IsChecked) ||
(ckbx4.IsChecked));
}
Run Code Online (Sandbox Code Playgroud)
...我已经在我的轨道上停了下来
Operator '||' cannot be applied to operands of type 'bool?' and 'bool?
Run Code Online (Sandbox Code Playgroud)
那我该怎么做呢?
Jon*_*eet 31
您可以|使用末尾的null-coalescing运算符将s 链接在一起:
return (ckbx1.IsChecked | cxbx2.IsChecked | cxbx3.IsChecked | cxbx4.IsChecked) ?? false;
Run Code Online (Sandbox Code Playgroud)
如果两个操作数都是,那么提升的|运算符返回,如果两个操作数都是,则操作数是,而另一个不是.truetruefalsefalsenullnulltrue
这不是短路,但在这种情况下,我不认为这对你来说是个问题.
或者 - 更可扩展 - 将复选框放入某种集合中.然后你可以使用:
return checkboxes.Any(cb => cb.IsChecked ?? false);
Run Code Online (Sandbox Code Playgroud)
Joe*_*Joe 11
尝试:
return ((ckbx1.IsChecked ?? false) ||
(ckbx2.IsChecked ?? false) ||
...
Run Code Online (Sandbox Code Playgroud)
我假设如果为null,那么它将是假的,你可以使用?? 运营商.
private bool AtLeastOnePlatypusChecked()
{
return ((ckbx1.IsChecked ?? false) ||
(ckbx2.IsChecked ?? false) ||
(ckbx3.IsChecked ?? false) ||
(ckbx4.IsChecked ?? false));
}
Run Code Online (Sandbox Code Playgroud)
您可以使用GetValueOrDefault()获取值,或false.
private bool AtLeastOnePlatypusChecked()
{
return ((ckbx1.IsChecked.GetValueOrDefault()) ||
(ckbx2.IsChecked.GetValueOrDefault()) ||
(ckbx3.IsChecked.GetValueOrDefault()) ||
(ckbx4.IsChecked.GetValueOrDefault()));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
480 次 |
| 最近记录: |