Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Performance / November 2003

Tip: Looking for answers? Try searching our database.

Object Equality

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Doug Holland - 25 Nov 2003 21:29 GMT
Currently I am responsible for writing the C# coding standards document for
a client and I have been doing some investigations with ildasm to establish
some additional best practices.

When using either the C# equality operator '==' or the object.Equals()
methods, which is better?

Personally I prefer the object.Equals as it prevents accidental assignement
using only a single equals, however the C# '==' is certainly less to write
(not that that in itself justifies its use).

When you compile the code and examine the resultant IL using ildasm you see
that the C# '==' operator is not simply resolved to object.Equals ... as
such this begs the question, what is the difference between:

a == b

call bool [mscorlib /* 23000001 */]System.String/* 01000003
*/::op_Equality(string, string) /* 0A000002 */

and:

string.Equals(a, b);

call bool [mscorlib/* 23000001 */]System.String/* 01000003
*/::Equals(string, string) /* 0A000003 */

Is there any performance difference between the two equality methods?

Or any other reason to prefer one over the other?

Thanks in advance

Doug Holland
Jon Skeet [C# MVP] - 26 Nov 2003 07:19 GMT
> Is there any performance difference between the two equality methods?

Well, they do two very different things. Unless == has been overridden
(which I dislike, apart from a few very special and well-known cases),
== just checks whether the two values on either side are the same - and
for reference types, those values are references. In other words:

object x = (...);
object y = (...);

if (x==y)

checks whether or not x and y are equal references, i.e. whether the
values of x and y are references to the same object.

if (x.Equals(y))

checks whether or not the object x refers to is equal to the object y
refers to, where the class can decide what equality means, so that two
distinct objects can be equal.

> Or any other reason to prefer one over the other?

It depends whether you want to detect reference identity or object
equality.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Doug Holland - 26 Nov 2003 15:33 GMT
Thanks Jon

- Doug

> > Is there any performance difference between the two equality methods?
>
[quoted text clipped - 21 lines]
> It depends whether you want to detect reference identity or object
> equality.
Mark - 26 Nov 2003 19:55 GMT
> It depends whether you want to detect reference identity or object
> equality.

Jon, have you thought about writing up a page for your site that goes into
these == and .Equals() issues? I refer people to your parameters page all
the time, and I think you'd do a great job on this other often-
misunderstood facet of the language.

Mark
Jon Skeet [C# MVP] - 26 Nov 2003 23:10 GMT
> > It depends whether you want to detect reference identity or object
> > equality.
[quoted text clipped - 3 lines]
> the time, and I think you'd do a great job on this other often-
> misunderstood facet of the language.

Good idea. I'll also include it in the FAQ that I've been trying to get
round to compiling...

(Too many pages to write, too little time :)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Richard A. Lowe - 27 Nov 2003 09:07 GMT
Actually Jon, perhaps you should go with the .NET / Java zeitgeist and start
a blog :)  I'd be happy to set you up with a .Text based one (though I
imagine MS gives it's MVPs free ones?)

{*Really* Off topic: As far as exposure, you can't beat it... I only get 30
unique visitors a day, but I'm #6 on Google for "C# .NET blogs" - right
behind Don Box at the moment).

Richard
Signature

Veuillez m'excuser, mon Fran?ais est tr?s pauvre.  Cependant, si vous voyez
mauvais C #, c'est mon d?faut!

> >
> > > It depends whether you want to detect reference identity or object
[quoted text clipped - 9 lines]
>
> (Too many pages to write, too little time :)
Jon Skeet [C# MVP] - 27 Nov 2003 09:36 GMT
> Actually Jon, perhaps you should go with the .NET / Java zeitgeist and start
> a blog :)  I'd be happy to set you up with a .Text based one (though I
> imagine MS gives it's MVPs free ones?)

Yes, I think so. Must check.

> {*Really* Off topic: As far as exposure, you can't beat it... I only get 30
> unique visitors a day, but I'm #6 on Google for "C# .NET blogs" - right
> behind Don Box at the moment).

I've thought about it - and I'll think about it some more. Thing is,
I'm always "tweaking" my normal pages, and I don't know how easy that
is to do with blogs. Maybe I should investigate them further...

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Richard A. Lowe - 27 Nov 2003 17:37 GMT
Guess it depends on the the software... .in .Text it's easy to edit posts
but I imaging there's some very mature JSP open-source for blogging as well.
I do like that .Text does automatic trackbacks when you link to other blogs
posts and esp. like blogs I can subscribe to via RSS.

R.

Signature

Veuillez m'excuser, mon Fran?ais est tr?s pauvre.  Cependant, si vous voyez
mauvais C #, c'est mon d?faut!

> > Actually Jon, perhaps you should go with the .NET / Java zeitgeist and start
> > a blog :)  I'd be happy to set you up with a .Text based one (though I
[quoted text clipped - 9 lines]
> I'm always "tweaking" my normal pages, and I don't know how easy that
> is to do with blogs. Maybe I should investigate them further...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.