473,394 Members | 1,971 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,394 software developers and data experts.

Override Equals ... AND Hashcode?

I've built a class and overroad the Equals method. I've gotten the warning
below. I'm unfamiliar with the "GetHashCode" method. An explantion of the
method and its relation to the warning would be appreciated.

warning: 'myClass' overrides Object.Equals(object o) but does not override
Object.GetHashCode()

Thanks in advance.

Mark
Nov 16 '05 #1
2 9570
Mark <Ma**@nowhere.com> wrote:
I've built a class and overroad the Equals method. I've gotten the warning
below. I'm unfamiliar with the "GetHashCode" method. An explantion of the
method and its relation to the warning would be appreciated.

warning: 'myClass' overrides Object.Equals(object o) but does not override
Object.GetHashCode()


Basically, if you override Equals but don't override GetHashCode, your
object may not work properly when put into a HashTable. See the docs on
GetHashCode for details of it.

Personally I'd suggest ignoring the requirement to make the hash code
constant after construction - if the object is mutable and changes in a
way which affects equality, it makes sense for the hash code to change
too. However, that does mean that a hash table may not be able to look
up the object using even the same reference if you change the contents
of the object after putting it into the 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
> I'm unfamiliar with the "GetHashCode" method.

GetHashCode is used by hash tables (like System.Collections.Hashtable) to
decide where to put your data when you insert it into the table.

It is also used to make a "first cut" at doing a lookup; that is, the hash
table uses GetHashCode to help it find your data in the table. It uses
GetHashCode to eliminate most of the entries without looking at them. Then it
uses the Equals method to search through those that are left. Thus,
GetHashCode and Equals are related and the compiler reminds you to please
override both if you override either.

One final note about System.Collections.Hashtable: it stores <key,value>
pairs. When you insert, the table calls GetHashCode on the key to help it
decide where to store your data in the table. When you lookup, it calls
GetHashCode and Equals on the key. If you use your objects as the key in
Hashtable, then lookups can fail if GetHashCode and Equals don't work
together (e.g. Equals objects must return the same thing from GetHashCode).

This is a pretty big/confusing topic - hope that gets you started.

Mark
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Murat Tasan | last post by:
i have a class, in which i have override the Object.equals(Object o) method... and then i try to make two equal objects (under the new definition) and place them into a HashSet... which should only...
1
by: pentium77 | last post by:
Hi, Just wondering what's the algo used to compute hashcode in Java ? Does anybody here know ? Thanks, -MK.
5
by: Stoyan | last post by:
Hi All, I don't understand very well this part of MSDN: "Derived classes that override GetHashCode must also override Equals to guarantee that two objects considered equal have the same hash code;...
8
by: MuZZy | last post by:
Hi, Why for god sake they change implementation of String.GetHashCode() from ..NET 1 to .NET 2? We were storing some user passwords in hashcode, now we can't upgrade those clients with .NET 2...
9
by: archana | last post by:
Hi all, I have one question regarding hashing in .net I have two string containing same data. When i see hashcode by calling gethascode, i am getting same value. I want to know how...
1
by: eramfelt | last post by:
Why doesnt ArrayList implements the Equals() or GetHashCode() methods? How is the developer supposed to check if an array list is equal to another array list? Is the developer supposed to iterate...
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...
28
by: Tony Johansson | last post by:
Hello! I can't figure out what point it is to use GetHashCode. I know that this GetHashCode is used for obtaining a unique integer value. Can somebody give me an example that prove the...
6
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I have a moderately complex class (about 10 different string and int type fields along with two different List<stringfields). I want to override Equals(), so I need to override GetHashCode()....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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.