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

前端 未结 7 2051
醉梦人生
醉梦人生 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

    They accomplish the same task.

    Only difference would be readability as to whether your coworkers or whomever is reading the code understands the syntax.

    EDIT: Additionally the first option can evaluate the property MyName twice.

提交回复
热议问题