Is there extra overhead in using the object.ReferenceEquals
method verses using ((object)obj1 == (object)obj2)
?
In the first case, there would be a static method call involved, and in both cases some form of casting to an object would be involved.
Even if the compiler balances out those methods, what about inequality?
(object)obj != null
as compared to...
!object.ReferenceEquals(obj,null)
I suppose that at some point, a logical negation would occur, either within the != operator, or as applied to the result of the ReferenceEquals method. What do you think?
There's also the issue of readability to consider. ReferenceEquals seems clearer when checking equality, but for inequality, one might miss the !
preceding object.ReferenceEquals
, whereas the !=
in the first variation is hard to overlook.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…