Equals(item, null) or item == null

后端 未结 6 1287
深忆病人
深忆病人 2020-12-07 23:14

Is code that uses the static Object.Equals to check for null more robust than code that uses the == operator or regular Object.Equals? Aren\'t the latter two vulnerable to b

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 23:31

    I ended up here when I was trying to compare the unique Id of objects that could themselves be null. Found it easier to just impute the missing data first, and then do the comparison.

    Guid currentId = (Object1 == null) ? Guid.Empty : Object1.Id;
    Guid newId = (Object2 == null) ? Guid.Empty : Object2.Id;
    If (currentId == newId)
    {
        //do happyface
    }
    else
    {
       //do sadface
    }
    

提交回复
热议问题