毫无疑问,还有其他可能更好的方法可以做到这一点,但我正在努力了解这里发生了什么。
在下面的示例中,coverity 在第四行报告了 FORWARD_NULL 缺陷。
double? foo = null;
double bar = 1.23;
foo += bar;
System.Windows.Point point = new System.Windows.Point(foo,bar);
它报告:
assign_zero: Assigning: foo = null.
在 foo += bar 行上。
在+= Operator (C# Reference) ,我看到 x += y 等同于 x = x + y,并且在 Using nullable types (C+ Programming Guide) 中, 我看到了
These operators [the binary operator] produce a null value if one or both operands are null
这是怎么回事? foo += bar 变为 foo = foo + bar 并且由于 foo 为空,foo + bar 为空?
最佳答案
so is that what is going on? foo += bar becomes foo = foo + bar and since foo is null, foo + bar is null?
是的。
关于c# - 为什么在可空类型上使用 += 会导致 FORWARD_NULL 缺陷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55066002/