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 184489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:18:05+00:00 2026-05-11T15:18:05+00:00

After reading all the questions and answers on StackOverflow concerning overriding GetHashCode() I wrote

  • 0

After reading all the questions and answers on StackOverflow concerning overriding GetHashCode() I wrote the following extension method for easy and convenient overriding of GetHashCode():

public static class ObjectExtensions {     private const int _seedPrimeNumber = 691;     private const int _fieldPrimeNumber = 397;     public static int GetHashCodeFromFields(this object obj, params object[] fields) {         unchecked { //unchecked to prevent throwing overflow exception             int hashCode = _seedPrimeNumber;             for (int i = 0; i < fields.Length; i++)                 if (fields[i] != null)                     hashCode *= _fieldPrimeNumber + fields[i].GetHashCode();             return hashCode;         }     } } 

(I basically only refactored the code that someone posted there, because I really like that it can be used generally)

which I use like this:

    public override int GetHashCode() {         return this.GetHashCodeFromFields(field1, field2, field3);     } 

Do you see any problems with this code?

  • 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-11T15:18:05+00:00Added an answer on May 11, 2026 at 3:18 pm

    That looks like a solid way to do it.

    My only suggestion is that if you’re really concerned about performance with it, you may want to add generic versions for several common cases (ie. probably 1-4 args). That way, for those objects (which are most likely to be small, key-style composite objects), you won’t have the overhead of building the array to pass to the method, the loop, any boxing of generic values, etc. The call syntax will be exactly the same, but you’ll run slightly more optimized code for that case. Of course, I’d run some perf tests over this before you decide whether it’s worth the maintenance trade-off.

    Something like this:

    public static int GetHashCodeFromFields<T1,T2,T3,T4>(this object obj, T1 obj1, T2 obj2, T3 obj3, T4 obj4) {     int hashCode = _seedPrimeNumber;     if(obj1 != null)         hashCode *= _fieldPrimeNumber + obj1.GetHashCode();     if(obj2 != null)         hashCode *= _fieldPrimeNumber + obj2.GetHashCode();     if(obj3 != null)         hashCode *= _fieldPrimeNumber + obj3.GetHashCode();     if(obj4 != null)         hashCode *= _fieldPrimeNumber + obj4.GetHashCode();     return hashCode; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading the answers to the question Calculate Code Metrics I installed the tool
After reading the Head First Design Patterns book and using a number of other
After reading this question , I was reminded of when I was taught Java
After reading a bit more about how Gnutella and other P2P networks function, I
After reading Practical Common Lisp I finally understood what the big deal about macros
After reading this description of late static binding (LSB) I see pretty clearly what
After reading Evan's and Nilsson's books I am still not sure how to manage
After reading What’s your/a good limit for cyclomatic complexity? , I realize many of
After reading this answer: best way to pick a random subset from a collection?
After reading the Test-and-Set Wikipedia entry , I am still left with the question

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.