我有以下代码:
var reportDate = (model.From?.Year == null || model.From?.Month == null || model.From?.Day == null) ?
DateTime.UtcNow.Date :
new DateTime(model.From.Year.Value, model.From.Month.Value, model.From.Day.Value);
注意条件表达式,最好是编写
(model.From == null || model.From.Year == null || model.From.Month == null || model.From.Day == null)
所以对于每个执行过的model.From
运算符,它们都不会是==
null检查?编译器是否知道如何优化前一代码只检查model.From
只为null一次?