c# - C# 中的空合并运算符和运算符 &&

标签 c# boolean-logic logical-operators null-coalescing-operator

是否可以在下一种情况下以任何方式一起使用运算符 ?? 和运算符 &&:

bool? Any
{
   get
   {
      var any = this.ViewState["any"] as bool?;
      return any.HasValue ? any.Value && this.SomeBool : any;
   }
}

这意味着下一步:

  • 如果 any 为空,则 this.Any.HasValue 返回 false
  • 如果 any 有值,那么它会返回考虑到另一个 bool 属性的值,即 Any && SomeBool

最佳答案

我想知道为什么到目前为止还没有人提出这个建议:

bool? any = this.ViewState["any"] as bool?;
return any & this.SomeBool;

返回

  • null 如果any 为null,无论this.SomeBool 的值是什么;
  • true 如果 anythis.SomeBool 都为真;和
  • false 如果 any 不为 null,并且 this.SomeBool 为 false。

关于c# - C# 中的空合并运算符和运算符 &&,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2725579/

相关文章:

javascript - 在 ES7 中使用求幂运算符的逆蕴涵逻辑?

java - 在 Java 中为列表过滤编写 AND 过滤器的简洁方法

python - 在python中查找指定范围内的图像像素的坐标

javascript - 如果输入 a 或 b 正确/正确,我如何执行操作?

r "any of"操作的逻辑条件

c# - 如何在 .net 的 sql server 中存储 float[]?

c# - 添加/删除示例的 MVVM 设计

Mac 上 xcode 的 C++ bool 逻辑语法

c# - 理解 C# 中的 IEnumerable<T>

c# - 在 DataGrid 中突出显示 SelectedItem 的行