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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 177k
  • Answers 177k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you can't write a program to find out, you… May 12, 2026 at 3:32 pm
  • Editorial Team
    Editorial Team added an answer user_id wasn't being set correctly. script/console: >> Membership.find(:first, :conditions =>… May 12, 2026 at 3:32 pm
  • Editorial Team
    Editorial Team added an answer It looks like you want to use Directory.GetFiles() in the… May 12, 2026 at 3:32 pm

Related Questions

We recently built a web app using Prototype, making a fair amount of use
Recently, I've got a dangerous idea into my head after reading this blog post.
I was reading What happens when a code signing certificate expires - Stack Overflow
I'm working on a fairly large project for a trading company in Philadelphia. The

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.