加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 为什么这个按位OR在可空int上返回null?

发布时间:2020-12-15 08:21:55 所属栏目:百科 来源:网络整理
导读:我基本上有以下内容: int? myVal = null; myVal |= 1; bool stillNull = myVal == null; //returns true 为什么这样做?我对按位运算符/操作数行为的理解并不是非常强大,我找不到在这种情况下不会将其视为简单赋值的原因. 解决方法 从 MSDN开始: The prede
我基本上有以下内容:
int? myVal = null;
    myVal |= 1;
    bool stillNull = myVal == null; //returns true

为什么这样做?我对按位运算符/操作数行为的理解并不是非常强大,我找不到在这种情况下不会将其视为简单赋值的原因.

解决方法

从 MSDN开始:

The predefined unary and binary operators and any user-defined operators that exist for value types may also be used by nullable types. These operators produce a null value if the operands are null; otherwise,the operator uses the contained value to calculate the result.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读