Are these two lines the same, '? … :' vs '??'?

前端 未结 7 2052
醉梦人生
醉梦人生 2021-01-30 19:28

Is there a difference between these two lines?

MyName = (s.MyName == null) ? string.Empty : s.MyName

or

MyName = s.MyName ?? st         


        
7条回答
  •  臣服心动
    2021-01-30 20:04

    The only difference is whether you evaluate s.MyName twice or once. The first will do it twice in the case that s.MyName is not null, the second will only ever evaluate it once.

    In most cases, this difference doesn't matter, and I'd go with the second because it's more clear and concise.

提交回复
热议问题