override GetHashCode

A

Andrew Robinson

I have a class that has three properties: two of type int and one of type
string.

Is this the best method when overriding the GetHashCode() ? I am guessing
not... any thing better?

public override int GetHashCode() {
string hash = craneCounterweightID.ToString() + ":" +
trailerID.ToString() + ":" + craneConfigurationTypeCode;
return hash.GetHashCode();
}



Thanks,
 
J

Jon Skeet [C# MVP]

Andrew Robinson said:
I have a class that has three properties: two of type int and one of type
string.

Is this the best method when overriding the GetHashCode() ? I am guessing
not... any thing better?

public override int GetHashCode() {
string hash = craneCounterweightID.ToString() + ":" +
trailerID.ToString() + ":" + craneConfigurationTypeCode;
return hash.GetHashCode();
}

No, the above is relatively slow and doesn't help a lot. Some people
use XOR (eg a ^ b ^ c) but I prefer the kind of method shown in Josh
Bloch's "Effective Java":

public override int GetHashCode()
{
int hash = 23;
hash = hash*37 + craneCounterweightID;
hash = hash*37 + trailerID;
hash = hash*37 + craneConfigurationTypeCode.GetHashCode();
return hash;
}

The 23 and 37 are arbitrary numbers which are co-prime.

The benefit of the above over the XOR method is that if you have a type
which has two values which are frequently the same, XORing those values
will always give the same result (0) whereas the above will
differentiate between them unless you're very unlucky.
 
B

Brian Gideon

I have a class that has three properties: two of type int and one of type
string.

Is this the best method when overriding the GetHashCode() ? I am guessing
not... any thing better?

public override int GetHashCode() {
string hash = craneCounterweightID.ToString() + ":" +
trailerID.ToString() + ":" + craneConfigurationTypeCode;
return hash.GetHashCode();

}

Thanks,

Hi,

XOR'ing the constituent fields provides a fairly good and fast
implementation.

public override int GetHashCode()
{
return
craneCounterweightID.GetHashCode() ^
trailerID.GetHashCode() ^
craneConfigurationTypeCode.GetHashCode();
}

One problem with XOR is that it is commutative. That means if the
values for craneCounterweightID and trailerID are swapped then the
returned code will remain the same. Addition and multiplication are
commutative as well so using either of those operators by themselves
will cause the same behavior not to mention that they probably
wouldn't produce a good distribution of values anyway. Basically, you
need to remove the "commutativeness" from the formula and still
maintain a good distribution. Jon posted this example some time ago.

public override int GetHashCode()
{
int result = 17;
result = result*37 + craneCounterweightID.GetHashCode();
result = result*37 + trailerID.GetHashCode();
result = result*37 + craneConfigurationTypeCode.GetHashCode();
return result;
}

Brian
 
L

Linda Liu [MSFT]

Hi Andrew,

I agree to what Jon has suggested.

In addition, while we override the GetHashCode method in a derived class,
we'd better also override the Equals method to guarantee that the two
objects considered equal have the same hash code; otherwise, Hashtable
might not work correctly.

Hope this helps.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Linda Liu [MSFT]

Hi Andrew,

How about the problem now?

If you have any question, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!


Sincerely,
Linda Liu
Microsoft Online Community Support
 

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