Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 68629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:26:47+00:00 2026-05-10T19:26:47+00:00

In .NET, the GetHashCode method is used in a lot of places throughout the

  • 0

In .NET, the GetHashCode method is used in a lot of places throughout the .NET base class libraries. Implementing it properly is especially important to find items quickly in a collection or when determining equality.

Is there a standard algorithm or best practice on how to implement GetHashCode for my custom classes so I don’t degrade performance?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:26:47+00:00Added an answer on May 10, 2026 at 7:26 pm

    I usually go with something like the implementation given in Josh Bloch’s fabulous Effective Java. It’s fast and creates a pretty good hash which is unlikely to cause collisions. Pick two different prime numbers, e.g. 17 and 23, and do:

    public override int GetHashCode() {     unchecked // Overflow is fine, just wrap     {         int hash = 17;         // Suitable nullity checks etc, of course :)         hash = hash * 23 + field1.GetHashCode();         hash = hash * 23 + field2.GetHashCode();         hash = hash * 23 + field3.GetHashCode();         return hash;     } } 

    As noted in comments, you may find it’s better to pick a large prime to multiply by instead. Apparently 486187739 is good… and although most examples I’ve seen with small numbers tend to use primes, there are at least similar algorithms where non-prime numbers are often used. In the not-quite-FNV example later, for example, I’ve used numbers which apparently work well – but the initial value isn’t a prime. (The multiplication constant is prime though. I don’t know quite how important that is.)

    This is better than the common practice of XORing hashcodes for two main reasons. Suppose we have a type with two int fields:

    XorHash(x, x) == XorHash(y, y) == 0 for all x, y XorHash(x, y) == XorHash(y, x) for all x, y 

    By the way, the earlier algorithm is the one currently used by the C# compiler for anonymous types.

    This page gives quite a few options. I think for most cases the above is "good enough" and it’s incredibly easy to remember and get right. The FNV alternative is similarly simple, but uses different constants and XOR instead of ADD as a combining operation. It looks something like the code below, but the normal FNV algorithm operates on individual bytes, so this would require modifying to perform one iteration per byte, instead of per 32-bit hash value. FNV is also designed for variable lengths of data, whereas the way we’re using it here is always for the same number of field values. Comments on this answer suggest that the code here doesn’t actually work as well (in the sample case tested) as the addition approach above.

    // Note: Not quite FNV! public override int GetHashCode() {     unchecked // Overflow is fine, just wrap     {         int hash = (int) 2166136261;         // Suitable nullity checks etc, of course :)         hash = (hash * 16777619) ^ field1.GetHashCode();         hash = (hash * 16777619) ^ field2.GetHashCode();         hash = (hash * 16777619) ^ field3.GetHashCode();         return hash;     } } 

    Note that one thing to be aware of is that ideally you should prevent your equality-sensitive (and thus hashcode-sensitive) state from changing after adding it to a collection that depends on the hash code.

    As per the documentation:

    You can override GetHashCode for immutable reference types. In general, for mutable reference types, you should override GetHashCode only if:

    • You can compute the hash code from fields that are not mutable; or
    • You can ensure that the hash code of a mutable object does not change while the object is contained in a collection that relies on its hash code.

    The link to the FNV article is broken but here is a copy in the Internet Archive: Eternally Confuzzled – The Art of Hashing

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to inherit from a base class, one method of which has a
I read in the Essential C# 3.0 and .NET 3.5 book that: GetHashCode()’s returns
If I understand correctly, in .NET the default implementation of Object.GetHashCode() returns a value
.NET has a lot of complex data structures. Unfortunately, some of them are quite
I have this function in .net code: public class StringGenerator { public static string
This is an Example from MSDN about Object Class in .NET FrameWork. using namespace
When an object is added to the .NET System.Collections.Generic.Dictionary class the hashcode of the
Why GetHashCode is not a property like HashCode in .NET?
We are currently extensively using the GetHashCode method to store hash codes in a
Enviroment: asp.net framework 2.0 I'm having problems with the Delete method of and ObjectDataSource

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.