开发者

Object.Equals(obj1, obj2) vs obj1.Equals(obj2)?

开发者 https://www.devze.com 2022-12-08 04:47 出处:网络
Assuming both objects are not value types and both represent types which have overridden the Equals(...) method, is there any functional difference between:

Assuming both objects are not value types and both represent types which have overridden the Equals(...) method, is there any functional difference between:

  • Calling obj开发者_高级运维1.Equals(obj2)
  • Calling Object.Equals(obj1, obj2) ...or are they functionally the same?

The Apress book I'm reading (Pro C# 2008), which is actually quite good, refers to this method (as well as ReferenceEquals(...) ) as "(very helpful) static methods", but I'm struggling to see the benefit here.

For ReferenceEquals(...) I can see the utility, as it is still capable of determining if two objects reference the same object (regardless of whether the Equals(...) method and and the == operator have been overridden).

For the other...not so much; Am I just missing something here?


Object.Equals(obj1, obj2):

if obj1 and obj2 are the same reference, returns true

if obj1 or obj2 is null, return false

otherwise returns obj1.Equals(obj2)


Imagine if, in the first case, obj1 was null.


Have a look at http://msdn.microsoft.com/en-us/library/w4hkze5k.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消