473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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 5632
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object.cs in rotor. What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What I don't...
0
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object class (Object.cs in rotor). What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What...
4
by: Arjen | last post by:
Hi, I have a class with some attributes. For example class person with name as attribute. I have add multiple persons in an arraylist. Now I need a function to get/find a person by the name...
2
by: One Handed Man [ OHM# ] | last post by:
The help for the .NET Framework Class Library tells us that the Object.GetHashCode() Method does not guarantee uniqueness' or consistency and that overriding this and the Equals method is a good...
6
by: Daniel Klein | last post by:
A simple question hopefully... Can the Object GetHashCode() method be used to obtain a unique identifier for an instance of an object? To put this another way, what does the...
3
by: Sam | last post by:
Hi Everyone, I have a stucture below stored in an arraylist and I want to check user's input (point x,y) to make sure there is no duplicate point x,y entered (string label can be duplicated). Is...
1
by: MariusI | last post by:
I have some business objects which overrides Equals to provide syntax equality instead of just reference equality. Overriding equals gives me a warning that i must override GetHashcode(). Msdn says...
5
by: taumuon | last post by:
I've got an object, Person, that supports IEquatable<Person>. It implements bool Equals(Person obj) as well as overriding bool Equals(object obj) I've got a container type that holds a member...
3
by: Al Meadows | last post by:
I'm using a generic Dictionary object where it is described as: Dictionary<this, thatstuff = new Dictonary<this, that>() Where THIS is a class with several property values. This works fine...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.