Groups | Blog | Home
all groups > dotnet performance > november 2003 >

dotnet performance : Object Equality


Doug Holland
11/25/2003 1:29:32 PM
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]
11/26/2003 7:19:29 AM
[quoted text, click to view]

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.

[quoted text, click to view]

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

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Doug Holland
11/26/2003 7:33:05 AM
Thanks Jon

- Doug

[quoted text, click to view]

Mark
11/26/2003 11:55:04 AM
Jon Skeet [C# MVP] <skeet@pobox.com> wrote in
news:MPG.1a2e64583cd14d5d989b3a@msnews.microsoft.com:

[quoted text, click to view]

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.

Jon Skeet [C# MVP]
11/26/2003 11:10:42 PM
[quoted text, click to view]

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 :)

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Richard A. Lowe
11/27/2003 3:07:16 AM
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
--
Veuillez m'excuser, mon Français est très pauvre. Cependant, si vous voyez
mauvais C #, c'est mon défaut!
[quoted text, click to view]

Jon Skeet [C# MVP]
11/27/2003 9:36:52 AM
[quoted text, click to view]

Yes, I think so. Must check.

[quoted text, click to view]

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...

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Richard A. Lowe
11/27/2003 11:37:01 AM
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.

--
Veuillez m'excuser, mon Français est très pauvre. Cependant, si vous voyez
mauvais C #, c'est mon défaut!
[quoted text, click to view]

AddThis Social Bookmark Button