gpt4 book ai didi

c# - 为什么这个按位或在可为空的 int 上返回 null?

转载 作者:太空狗 更新时间:2023-10-29 21:22:56 25 4
gpt4 key购买 nike

我基本上有以下内容:

    int? myVal = null;
myVal |= 1;
bool stillNull = myVal == null; //returns true

为什么会这样?我对按位运算符/操作数行为的理解不是很强,我找不到在这种情况下不将其视为简单赋值的理由。

最佳答案

来自 the documentation :

The predefined unary and binary operators or any overloaded operatorsthat are supported by a value type T are also supported by thecorresponding nullable value type T?. These operators, also known aslifted operators, produce null if one or both operands are null; otherwise, the operator uses the contained values of its operands to calculate the result.

关于c# - 为什么这个按位或在可为空的 int 上返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17050661/

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