空合并操作符??(C#)

??二元操作符在对first??second求值时,大致会经历以下步骤:

1)对first进行求值;

2)如果结果非空,则该结果就是整个表达式的结果;

3)否则求second的值,其结果作为整个表达式的结果。

例如:

1 DateTime birth;
2 DateTime? death;
3 
4 public TimeSpan Age{
5      get{
6          return (death??DateTime.Now)-birth;
7      }
8 }

 

1 Address contact = user.ContactAddress??
2                   order.ShippingAddress??
3                   user.BillingAddress;

 

posted @ 2016-04-04 16:42  P_Chen  阅读(360)  评论(0编辑  收藏  举报