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

writing own GetHashCode() function

RAM
Hello,
I would like to ask programmers such question:
I have a class containing a few fields:
int i;
string a;
bool b;
int j;
bool q;
decimal d;
I need to write GetHashCode function for this class. Could you suggest
me some idea? Maybe I can should use one of the following solutions:

http://en.wikipedia.org/wiki/List_of...undancy_checks

Please help.
RAM
Aug 1 '07 #1
5 1876
RAM <r_********@poczta.onet.plwrote:
Hello,
I would like to ask programmers such question:
I have a class containing a few fields:
int i;
string a;
bool b;
int j;
bool q;
decimal d;
I need to write GetHashCode function for this class. Could you suggest
me some idea? Maybe I can should use one of the following solutions:

http://en.wikipedia.org/wiki/List_of...undancy_checks
I tend to use the following approach, as recommended by Josh Bloch in
"Effective Java":

int hash = 23;
hash = hash*37 + i;
hash = hash*37 + a.GetHashCode();
hash = hash*37 + (b ? 1 : 0);
hash = hash*37 + j;
hash = hash*37 + (q ? 1 : 0);
hash = hash*37 + d.GetHashCode();
return hash;

The 23 and 37 are preferrably prime numbers (or at least coprime) but I
don't think it matters too much what they are apart from that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 1 '07 #2
RAM wrote:
Hello,
I would like to ask programmers such question:
I have a class containing a few fields:
int i;
string a;
bool b;
int j;
bool q;
decimal d;
I need to write GetHashCode function for this class. Could you suggest
me some idea?
Depends on what you're actually storing. Quick and dirty could be:

return (i + "$" + a + "$" + b + "$" + j + "$" + q + "$" + d).GetHashCode();

Alun Harford
Aug 4 '07 #3
I suggest:

return i.GetHashCode() ^ a.GetHashCode() ^ b.GetHashCode() ^ etc.

Alberto

Aug 5 '07 #4
in**@devdept.com <in**@devdept.comwrote:
I suggest:

return i.GetHashCode() ^ a.GetHashCode() ^ b.GetHashCode() ^ etc.
That has issues for symmetry reasons. Consider the simple Point:

class Point
{
int x;
int y;
}

(They could be doubles, etc - all accessors etc removed.)

Using a hash built from x ^ y means that:

1) All points where x=y have the same hash - and they're not hugely
unlikely in the real world

2) A point (x, y) has the same hash as the point (y, x) which is again
not terribly unlikely to come up.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 5 '07 #5
Hi Jon,

I never thought about it. BTW we use floats and it becomes quite
unlikely.

Thanks for your post.

Alberto
Aug 6 '07 #6

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

Similar topics

4
by: Greg Bacchus | last post by:
Hi, I'm just concerned about storing a large amount of items in a Hashtable. It seems to me that as the number of keys in the Hashtable increases, so does the chance of key clashes. Does anyone...
7
by: Avin Patel | last post by:
Hi I have question for GetHashCode() function, Is it correct in following code or there is more efficient way to implement GetHashCode() function class IntArray public int data public...
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: Matthias Kientz | last post by:
I have a class which should override the GetHashcode() function, which look like this: public class A { public string str1; public string str2; //other methods...
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...
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...
1
by: Stephan Keil | last post by:
Hi all, I am somewhat confused by the Object.GetHashCode() documentation. What I am looking for, is a hash code, which is bound to the _identity_ of an object, not to it's _value_ (see other...
5
by: Metaman | last post by:
I'm trying to write a generic method to generate Hashcodes but am having some problems with (generic) collections. Here is the code of my method: public static int GetHashCode(object input) {...
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...
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
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?
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.