Equals(item, null) or item == null

后端 未结 6 1222
深忆病人
深忆病人 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:41

    The framework guidelines suggest that you treat Equals as value equality (checking to see whether two objects represent the same information, i.e. comparing properties), and == as reference equality, with the exception of immutable objects, for which you should probably override == to be value equality.

    So, assuming the guidelines apply here, pick whichever is semantically sensible. If you're dealing with immutable objects, and you expect both methods to produce identical results, I'd use == for clarity.

提交回复
热议问题