Home All Groups Group Topic Archive Search About

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

Author
19 Jul 2006 8:12 PM
Anthony
Hi All,
   I would like to know what is the differnet between operator comparision
and '.Equals(x)'

Anthony

Author
19 Jul 2006 10:10 PM
Barry Kelly
Anthony <Anth***@discussions.microsoft.com> 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

Author
20 Jul 2006 1:16 PM
Anthony
Thanks Barry, This is very helpful.

Show quote
"Barry Kelly" wrote:

> Anthony <Anth***@discussions.microsoft.com> 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/
>

AddThis Social Bookmark Button