Re: Different between operator comparision and '.Equals(x)'



Anthony <Anthony@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

I would like to know what is the differnet between operator comparision
and '.Equals(x)'

The '==' operator for reference types is by default a reference
comparison. Only if the two values refer to the same object does it
return true, unless it has been overloaded. If '==' has been overloaded,
the version of '==' called is statically determined at compile time
based on the 'operator ==' definitions on the two types involved (one on
each side of the '==').

By default, '==' isn't defined for value types (structs), unless you
define one yourself.

The Equals method is virtual, and thus is selected by dynamic dispatch
at runtime. By default, it also performs a reference comparison, but it
may be overloaded. By the way, to ease the complications of checking for
null and avoiding calling Equals on a null reference, you can use
object.Equals(object,object) to call Equals and it can take care of
those details.

-- Barry

--
http://barrkel.blogspot.com/
.



Relevant Pages

  • The truth about decibels
    ... equals 20 log x/y, where x and y are the different signal levels]. ... then it must be relative to some 0 dB reference point. ... was labeled dBv but was too often confused with dBV ... +4 dBu Standard pro audio voltage reference level equal to 1.23 Vrms. ...
    (sci.electronics.design)
  • Re: A better cloning mechanism
    ... > So it seems that if equals() were removed from Object, ... that do not need object semantics. ... I have often in the past argued that, in fact, the '==' operator in Java ... answer and you are only doing the reference comparison for performance. ...
    (comp.lang.java.programmer)
  • Re: performance of HashSet with strings and integers
    ... whose hash code is equal to the probe's hash code but that is not the ... and so the equals test is fast. ... mismatch or reference equality prevents execution of the equals call. ...
    (comp.lang.java.programmer)
  • Re: Comment on how to uniquely track your objects in C# / hash table / get hash code
    ... reference objects). ... the array will not involve comparing the respective hashcodes, ... will only compare the pointers to the objects, that is, their ... you can override) Equals is simply a comparison of references, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Set and .equals() semantics
    ... The input-reading method grabs a line of input, parses it, and creates a "Foo foo = new Foo". ... If an equivalent instance (as determined by equals()) is already present in the set, I want to let the new one go out of scope. ... If this is the first time I've seen it, everything is fine because I already have the reference and can add it to both places, e.g. ... Your canonicalisation step is in fact taking different objects and giving you one particular object, so that looks like a job for a Map to me. ...
    (comp.lang.java.programmer)

Loading