Object.Equals() and GethashCode()

C

cmrchs

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

Jon Skeet [C# MVP]

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

Richard Blewett [DevelopMentor]

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/<#ZfCwB#[email protected]>

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]
 
J

Jon Skeet [C# MVP]

Richard Blewett said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top