override GetHashCode

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Robinson

    override GetHashCode

    I have a class that has three properties: two of type int and one of type
    string.

    Is this the best method when overriding the GetHashCode() ? I am guessing
    not... any thing better?

    public override int GetHashCode() {
    string hash = craneCounterwei ghtID.ToString( ) + ":" +
    trailerID.ToStr ing() + ":" + craneConfigurat ionTypeCode;
    return hash.GetHashCod e();
    }



    Thanks,

  • Brian Gideon

    #2
    Re: override GetHashCode

    On Mar 14, 1:40 pm, "Andrew Robinson" <nem...@nospam. nospamwrote:
    I have a class that has three properties: two of type int and one of type
    string.
    >
    Is this the best method when overriding the GetHashCode() ? I am guessing
    not... any thing better?
    >
    public override int GetHashCode() {
    string hash = craneCounterwei ghtID.ToString( ) + ":" +
    trailerID.ToStr ing() + ":" + craneConfigurat ionTypeCode;
    return hash.GetHashCod e();
    >
    }
    >
    Thanks,
    Hi,

    XOR'ing the constituent fields provides a fairly good and fast
    implementation.

    public override int GetHashCode()
    {
    return
    craneCounterwei ghtID.GetHashCo de() ^
    trailerID.GetHa shCode() ^
    craneConfigurat ionTypeCode.Get HashCode();
    }

    One problem with XOR is that it is commutative. That means if the
    values for craneCounterwei ghtID and trailerID are swapped then the
    returned code will remain the same. Addition and multiplication are
    commutative as well so using either of those operators by themselves
    will cause the same behavior not to mention that they probably
    wouldn't produce a good distribution of values anyway. Basically, you
    need to remove the "commutativenes s" from the formula and still
    maintain a good distribution. Jon posted this example some time ago.

    public override int GetHashCode()
    {
    int result = 17;
    result = result*37 + craneCounterwei ghtID.GetHashCo de();
    result = result*37 + trailerID.GetHa shCode();
    result = result*37 + craneConfigurat ionTypeCode.Get HashCode();
    return result;
    }

    Brian

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: override GetHashCode

      Andrew Robinson <nemoby@nospam. nospamwrote:
      I have a class that has three properties: two of type int and one of type
      string.
      >
      Is this the best method when overriding the GetHashCode() ? I am guessing
      not... any thing better?
      >
      public override int GetHashCode() {
      string hash = craneCounterwei ghtID.ToString( ) + ":" +
      trailerID.ToStr ing() + ":" + craneConfigurat ionTypeCode;
      return hash.GetHashCod e();
      }
      No, the above is relatively slow and doesn't help a lot. Some people
      use XOR (eg a ^ b ^ c) but I prefer the kind of method shown in Josh
      Bloch's "Effective Java":

      public override int GetHashCode()
      {
      int hash = 23;
      hash = hash*37 + craneCounterwei ghtID;
      hash = hash*37 + trailerID;
      hash = hash*37 + craneConfigurat ionTypeCode.Get HashCode();
      return hash;
      }

      The 23 and 37 are arbitrary numbers which are co-prime.

      The benefit of the above over the XOR method is that if you have a type
      which has two values which are frequently the same, XORing those values
      will always give the same result (0) whereas the above will
      differentiate between them unless you're very unlucky.

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Linda Liu [MSFT]

        #4
        RE: override GetHashCode

        Hi Andrew,

        I agree to what Jon has suggested.

        In addition, while we override the GetHashCode method in a derived class,
        we'd better also override the Equals method to guarantee that the two
        objects considered equal have the same hash code; otherwise, Hashtable
        might not work correctly.

        Hope this helps.


        Sincerely,
        Linda Liu
        Microsoft Online Community Support

        =============== =============== =============== =====
        Get notification to my posts through email? Please refer to
        Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.

        ications.

        Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
        where an initial response from the community or a Microsoft Support
        Engineer within 1 business day is acceptable. Please note that each follow
        up response may take approximately 2 business days as the support
        professional working with you may need further investigation to reach the
        most efficient resolution. The offering is not appropriate for situations
        that require urgent, real-time or phone-based interactions or complex
        project analysis and dump analysis issues. Issues of this nature are best
        handled working with a dedicated Microsoft Support Engineer by contacting
        Microsoft Customer Support Services (CSS) at
        http://msdn.microsoft.com/subscripti...t/default.aspx.
        =============== =============== =============== =====

        This posting is provided "AS IS" with no warranties, and confers no rights.

        Comment

        • Linda Liu [MSFT]

          #5
          RE: override GetHashCode

          Hi Andrew,

          How about the problem now?

          If you have any question, please feel free to let me know.

          Thank you for using our MSDN Managed Newsgroup Support Service!


          Sincerely,
          Linda Liu
          Microsoft Online Community Support

          Comment

          Working...