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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:19:23+00:00 2026-06-12T00:19:23+00:00

I got reading this question here which led me to this article here .

  • 0

I got reading this question here which led me to this article here.

I’ve got an abstract base class that allows me to constrain methods to accept only my classes that extend my abstract base class (basic polymorphism). My question is: Can I implement GetHashCode() in my abstract base class to provide a suitable override for any concrete implementation? (I.e. to avoid overriding GetHashCode() in each concrete class.)

I’m imagining a method in my abstract base class something like this:

public abstract class FooBase
{
    private static readonly int prime_seed = 13;
    private static readonly int prime_factor = 7;

    public override int GetHashCode()
    {
        // Seed using the hashcode for this concrete class' Type so
        // two classes with the same properties return different hashes.
        int hash = prime_seed * this.GetType().GetHashCode();
        // Get this concrete class' public properties.
        var props = this.GetType().GetProperties(BindingFlags.Public);
        foreach (var prop in props)
        {
            // Factor in each of this concrete class' public properties' hashcodes.
            hash = (hash * prime_factor) + prop.GetHashCode();
        }
        return hash;
    }
}

This seems to work in some basic equality unit tests but I feel like I’ve overlooked something. I still have to provide an override in each concrete class to avoid the compiler Warning about not overriding GetHashCode(), but at least this way I don’t have to manually write an implementation for each one.

  • 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. Editorial Team
    Editorial Team
    2026-06-12T00:19:25+00:00Added an answer on June 12, 2026 at 12:19 am

    Does this perform better than:

    public override int GetHashCode()
    {
        return 1;
    }
    

    A key to a Hash function is it must be fast to compute. With reflection, you might lose all the benefits you stand to gain from having a hash.

    It would be worth it to benchmark.

    Also, hash codes must be equal when Equals returns true, so will all subclasses use only public properties in their Equals method? If not, you might consider looping over all properties, not just public ones.

    Edit to add:
    Also, consider adding unchecked around your property loop to prevent an exception if the hash gets bigger than Int.MaxValue.

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

Sidebar

Related Questions

After reading this article on thedailywtf.com, I'm not sure that I really got the
Thanks in Advance for reading and answer this question. I got button in asp
While reading the answers to this question I got a doubt regarding the default
Reading through this excellent article about safe construction techniques by Brain Goetz, I got
Hello I was reading this question, I got confused with how if we can
I posted this question on the Qt forum, but got no answers. That's why
I got this error Unhandled exception at 0x0049b946 in Program.exe: 0xC0000005: Access violation reading
After reading Eric Lippert’s answer I got the impression that await and call/cc are
Reading some questions here on SO about conversion operators and constructors got me thinking
I've got a bunch of csv files that I'm reading into R and including

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.