|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Different between operator comparision and '.Equals(x)'Hi All,
I would like to know what is the differnet between operator comparision and '.Equals(x)' Anthony Anthony <Anth***@discussions.microsoft.com> wrote:
> I would like to know what is the differnet between operator comparision The '==' operator for reference types is by default a reference> and '.Equals(x)' 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 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/ > |
|||||||||||||||||||||||