3

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?

Community
  • 1
  • 1
S2S2
  • 8,322
  • 5
  • 37
  • 65
  • 3
    Hashes arent required to be unique just that if a.Equals(b) then a.GetHashCode() == b.GetHashCode() – cordialgerm May 19 '12 at 18:49
  • 2
    This has been asked many times, search. And the best advice is probably: do not override Equals et al. – H H May 19 '12 at 18:51
  • You are unlikely to receive a generic answer to what the "optimal" hash function would look like. It very much depends on your definition of equality and what your object looks like in general. I would suggest you look around the site to see how others have implemented `GetHashCode` and then ask a specific question to a particular use case. – BrokenGlass May 19 '12 at 18:51
  • @BrokenGlass I have edited and provided a use case..I would also suggest to wait for a considerable time before closing the questions and give time to edit the questions...Thanks – S2S2 May 19 '12 at 19:17
  • @HenkHolterman I have added a code scenario and would like this question to be reopened and reexamined for answers and thoughts.. – S2S2 May 19 '12 at 19:22
  • You've posted a mutable class with _public fields_ and without a proper context for Equals(). Not good enough. If that's the real class, don't override. – H H May 19 '12 at 19:30
  • @HenkHolterman thats just a sample class to demo the scenario, in real case it would be an immutable class with read-only fields and constructor initialization.. – S2S2 May 19 '12 at 19:42
  • @pickles I suppose the `GetHashCode` implementation of `Object` always produces a unique hashcode for the current AppDomain – S2S2 May 19 '12 at 19:46
  • No, it should not be unique... in GetHashCode() you could just return your [laptop_model_name].GetHashCode() and it will be enough. Equals could be implemented just as you said(plus it should check [laptom_model_name]) – 6opuc May 20 '12 at 03:50
  • @CSharpVJ, hash codes are not guaranteed unique at all. –  May 20 '12 at 17:35

1 Answers1

1

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...

6opuc
  • 1,176
  • 3
  • 13
  • 21