473,796 Members | 2,741 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1889
RAM <r_********@poc zta.onet.plwrot e:
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.co m>
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.co m <in**@devdept.c omwrote:
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.co m>
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
429
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 know about the performance with regards to this? Cheers Greg
7
6576
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 override int GetHashCode() int hash = 0 for (int i = 0; i < data.Length; i++
5
9606
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; otherwise, Hashtable might not work correctly." Does any one know, why we must also override Equals, Please give my an example:) Thanks, Stoyan
8
9912
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
2378
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 idea. It also tells us that using the XOR functions on two or more Fields or Properties is an acceptable way to achieve this. What do you guys think is the best approach to this given that generating a hashcode should be fast as well as consistant....
1
2205
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 this about implementing hashcode: (1)If two objects of the same type represent the same value, the hash function must return the same constant value for either object. (2)For the best performance, a hash function must generate a random...
1
1305
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 thread "Object identity"). In the documentation, the statement "The hash function must return exactly the same value regardless of any changes that are made to the object" suggestes that the hash code is bound to an object _identity_ (that would
5
2285
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) { try { Type objectType = input.GetType(); PropertyInfo properties = objectType.GetProperties();
28
3789
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 usefulness of this GetHashCode or it it of no use at all? A mean that if I get an integer from current time in some way what should I use it for?
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9535
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10467
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10201
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6802
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5454
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.