468,133 Members | 1,138 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,133 developers. It's quick & easy.

Object.Equals() and GethashCode()

Hi,

why is it requested that when Equals() is implemented in a class that GethashCode() must be implemented as well ?

thnx
Chris

************************************************** ********************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
Nov 16 '05 #1
3 5410
<Chris C (cm****@yahoo.com)> wrote:
why is it requested that when Equals() is implemented in a class that
GethashCode() must be implemented as well ?


Basically, if Equals() is overridden but GetHashCode() isn't overridden
in a complementary way, instances of the class can't safely be used as
keys in a hashtable.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
The rule basically says:

if two object are deemed Equal then GetHashCode must return the same value (the converse is not true). So, if you override Equals the compiler forces you to take account of the this by overriding GetHashCode. Also, the value returned by GetHashCode must be immutable after construction. If it were not the bucket that an object was in in a HashTable may become invalid as the object's state changed.

Regards

Richard Blewett - DevelopMentor

http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<#Z**************@TK2MSFTNGP09.phx.gbl>

Hi,

why is it requested that when Equals() is implemented in a class that GethashCode() must be implemented as well ?

thnx
Chris

************************************************** ********************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #3
Richard Blewett [DevelopMentor] <ri******@develop.com> wrote:
The rule basically says:

if two object are deemed Equal then GetHashCode must return the same
value (the converse is not true). So, if you override Equals the
compiler forces you to take account of the this by overriding
GetHashCode. Also, the value returned by GetHashCode must be
immutable after construction. If it were not the bucket that an
object was in in a HashTable may become invalid as the object's state
changed.


The second rule seems like a bad one to me, I must say - it means that
you *can't* override Equals (and stick to the guidelines) for mutable
objects, unless you return a constant hashcode. There are a number of
cases where it's useful to have mutable objects which override Equals.

I take the line that it's fine to make the hash code derive from
mutable fields, but the client shouldn't expect an object which was
used as a key in a hashtable and then changed to behave properly.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Bijay Kumar | last post: by
reply views Thread by Bijay Kumar | last post: by
2 posts views Thread by One Handed Man [ OHM# ] | last post: by
6 posts views Thread by Daniel Klein | last post: by
3 posts views Thread by Al Meadows | last post: by
27 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.