J
Joey Callisay
Is there a difference between Object.ReferenceEquals() method and
the == operator comparing two reference types?
the == operator comparing two reference types?
Joey Callisay said:Is there a difference between Object.ReferenceEquals() method and
the == operator comparing two reference types?
Daniel O'Connell said:Read this article:
http://yoda.arachsys.com/csharp/faq/#equals
Everything there holds true to this, except Object.ReferenceEquals can be
used to guarentee referential equality in all cases, whereas Equals and ==
offer no guarenttees about how equality will be determined.
Joey Callisay said:Here is a revision of my question:
I have some old code here. A number of controls are linked to a common
eventhandler, since there is some interesection on the implementation. At
the eventhandler declaration, to test for the cases, it uses the Name
property of the controls and compare it to the sender's Name. I believe
this is not a good practice and comparing if both the control instance and
the sender points to the same control is a better implementation. My hunch
is to use the static Object.ReferenceEquals. Any views?
Jon Skeet said:It entirely depends on what semantics you want. If you want to be able
to construct a new control with an old name and still get a match, you
need to use name comparison, otherwise you could use
Object.ReferenceEquals or just == (assuming it hasn't been overloaded
for those types).
Jon Skeet said:Well, except that if either of the parameters is an expression of type
object, it's guaranteed that the result will be the same as
Object.ReferenceEquals. In other words:
x==(object)y is always the same as
Object.ReferenceEquals(x, y)