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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:24:17+00:00 2026-06-04T04:24:17+00:00

Possible Duplicate: What is the best algorithm for an overridden System.Object.GetHashCode? This is known

  • 0

Possible Duplicate:
What is the best algorithm for an overridden System.Object.GetHashCode?

This is known to us that if we override the Equals method of Object in our custom types, we should also override and provide an implementation of GetHashCode method to support generating unique hash codes for use in support of Hashtable and Dictionary collection classes and may be other classes.

This mandates our implementation of the hashing algorithm used inside our overriden GetHashCode method is optimal and accurate i.e. it generates a unique hash of the type and also does that as quickly as possible to improve performance of application that uses our type.

My question is to which hashing algorithms are accurate and give optimal performance when used in GetHashCode implementation? Or, should we only use the base type’s GetHashCode implementation? I would like to know this answer for both value types and reference types..

Edit: Here’s an example of a class below on why I would need to override Equals:

public class Laptop : LaptopBase
{
    public readonly string Make;
    public readonly string ProcessorArch;
    public readonly int MemorySupported;
    public readonly int HardDiskGBSupported;
    public readonly Color ColorName;

    public Laptop(make, procArch, memorySupp, hdGB, color)
    {
        Make = make;
        ProcessorArch = procArch;
        MemorySupported = memorySupp;
        HardDiskGBSupported = hdGB;
        ColorName = color;
    }
}

Now, I want to return true for 2 Laptop instances that have all the above defined fields matching with each other so that requires to override the Equals and GetHashCode method and also another requirement as you can see is that this is a derived class and may be re-used further and support a number of methods; and thus, cannot be made a value type (struct). I tried with 2 instances of above type with all matching instance fields and if I use the base implementation of Equals – it returns false where I want it to be true..
How would I support such a scenario?

  • 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-04T04:24:18+00:00Added an answer on June 4, 2026 at 4:24 am

    It depends on the type it is implemented on, but it should give good dispersion of values and it is NOT a must for GetHashCode() to return a unique values. It should base on those fields that are used in your Equals implementation and those fields should be immutable. So requirements for Equals/GetHashCode are the same for structs and classes.

    And as Henk said, it is better to not override Equals/GetHashCode at all…

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

Sidebar

Related Questions

Possible Duplicate: What is the best algorithm for an overridden System.Object.GetHashCode? What constitutes a
Possible Duplicate: What is the best algorithm for an overridden System.Object.GetHashCode? I need to
Possible Duplicate: Best practices in error reporting (Mathematica) Assume that I have a function
Possible Duplicate: Best practices regarding equals: to overload or not to overload? Does anyone
Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit
Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit
Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit
Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit
Possible Duplicate: Best Java obfuscator ? For my example I know that eclipse offers
Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit

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.