gpt4 book ai didi

c# - 十进制可空值和十进制值的总和

转载 作者:太空狗 更新时间:2023-10-30 00:10:18 26 4
gpt4 key购买 nike

为什么在此代码执行后变量 'a' 包含 null 而不是 10?

decimal? a = null;
a += 10;

最佳答案

您不能将值添加到 nullnull 将为 null 添加任何值。另一方面,如果您已将非空值分配给 a,则 10 将被添加到 a

取自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. For example:

int? a = 10;
int? b = null;

a++; // Increment by 1, now a is 11.
a = a * 10; // Multiply by 10, now a is 110.
a = a + b; // Add b, now a is null.

关于c# - 十进制可空值和十进制值的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28316761/

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