public class Calc { public long? Number { get; set; } public long Number1 { get; set; } public long Number2 { get; set; } } var calc = new Calc(); calc.Number1 = 100; calc.Number2 = 120; var result = calc.Number + calc.Number1+calc.Number2;
Resharper也没有提示代码有问题,但是这个代码得到的结果是错的 是null
结论:空对象加任何数字都是空对象
正确的做法:
var result = calc.Number.Hasvalue? calc.Number.value:0+ calc.Number1+calc.Number2;
结果是220
来源:https://www.cnblogs.com/hornet/p/4134642.html