why do we prefer ? to ?? operator in c#?

前端 未结 7 1951
别那么骄傲
别那么骄傲 2020-12-13 09:05

I recently found that we can use ?? operator to check nulls. Please check the below code samples:

   var res = data ?? new data();

This is

7条回答
  •  一向
    一向 (楼主)
    2020-12-13 09:45

    I would have thought the equivalent of

    var res = data ?? data.toString();
    

    would be

    var res = (data!=null) ? data : data.toString();
    

提交回复
热议问题