C#是否知道在多个条件语句中优化Null条件运算符?

时间:2016-11-11 02:30:42

标签: c# optimization null operators

我有以下代码:

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一次?

0 个答案:

没有答案