Object.GetHashCode()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guinness Mann

    Object.GetHashCode()

    resource.cs(8,1 5): warning CS0659: 'UA.LMS.Resourc e.Resource' overrides
    Object.Equals(o bject o) but does not override Object.GetHashC ode()

    What does this mean? I mean I know what it says, but do I need to do
    anything? If I specify exactly how to do the comparison why would I
    need to provide a hashcode override?

    Here are the interesting pieces of my class:

    public class Resource
    {
    public int rId;
    public string language;
    public string caption;

    (Various constructors elided)

    public static bool operator==(Reso urce lhs, Resource rhs)
    {
    if
    ( (lhs.rId == rhs.rId)
    && (lhs.language == rhs.language)
    && (lhs.caption == rhs.caption)
    )
    {
    return true;
    }

    return false;
    }

    public static bool operator !=(Resource lhs, Resource rhs)
    {
    return !(lhs == rhs);
    }

    public override bool Equals(object obj)
    {
    if (! (obj is Resource))
    return false;

    return this == (Resource)obj;
    }

    }
  • Ray Hsieh (Ray Djajadinata)

    #2
    Re: Object.GetHashC ode()

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Because if you override Equals() but not GetHashCode(), that means you
    can have two equal objects, with two different hashcode. Now that's bad! :)

    Guinness Mann wrote:

    | resource.cs(8,1 5): warning CS0659: 'UA.LMS.Resourc e.Resource' overrides
    | Object.Equals(o bject o) but does not override Object.GetHashC ode()
    |
    | What does this mean? I mean I know what it says, but do I need to do
    | anything? If I specify exactly how to do the comparison why would I
    | need to provide a hashcode override?
    |
    | Here are the interesting pieces of my class:
    |
    | public class Resource
    | {
    | public int rId;
    | public string language;
    | public string caption;
    |
    | (Various constructors elided)
    |
    | public static bool operator==(Reso urce lhs, Resource rhs)
    | {
    | if
    | ( (lhs.rId == rhs.rId)
    | && (lhs.language == rhs.language)
    | && (lhs.caption == rhs.caption)
    | )
    | {
    | return true;
    | }
    |
    | return false;
    | }
    |
    | public static bool operator !=(Resource lhs, Resource rhs)
    | {
    | return !(lhs == rhs);
    | }
    |
    | public override bool Equals(object obj)
    | {
    | if (! (obj is Resource))
    | return false;
    |
    | return this == (Resource)obj;
    | }
    |
    | }


    - --
    Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
    ray underscore usenet at yahoo dot com
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.3 (MingW32)
    Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

    iD8DBQE/oVNWwEwccQ4rWPg RAuW3AJ98MVeZtV 9DQwqWRLmqt0V/4jpvOACeMAUo
    4LBH2gqFoHd9tK5 WmeDhcwE=
    =UPTy
    -----END PGP SIGNATURE-----

    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Object.GetHashC ode()

      Ray and Guiness Mann,

      Furthermore, if your object is used as a key in a hashtable, then you
      will have two instances of the object, which are considered equal, filling
      two separate slots in the hashtable. This would be bad as well.

      When overriding GetHashCode, you can just get the hashcode of the three
      items you are comparing, and XOR all of them together, and that should give
      you a hashcode that you can return.

      Hope this helps.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m


      "Ray Hsieh (Ray Djajadinata)" <check@my.signa ture.com> wrote in message
      news:OKZkS0wnDH A.1740@TK2MSFTN GP12.phx.gbl...[color=blue]
      > -----BEGIN PGP SIGNED MESSAGE-----
      > Hash: SHA1
      >
      > Because if you override Equals() but not GetHashCode(), that means you
      > can have two equal objects, with two different hashcode. Now that's bad![/color]
      :)[color=blue]
      >
      > Guinness Mann wrote:
      >
      > | resource.cs(8,1 5): warning CS0659: 'UA.LMS.Resourc e.Resource' overrides
      > | Object.Equals(o bject o) but does not override Object.GetHashC ode()
      > |
      > | What does this mean? I mean I know what it says, but do I need to do
      > | anything? If I specify exactly how to do the comparison why would I
      > | need to provide a hashcode override?
      > |
      > | Here are the interesting pieces of my class:
      > |
      > | public class Resource
      > | {
      > | public int rId;
      > | public string language;
      > | public string caption;
      > |
      > | (Various constructors elided)
      > |
      > | public static bool operator==(Reso urce lhs, Resource rhs)
      > | {
      > | if
      > | ( (lhs.rId == rhs.rId)
      > | && (lhs.language == rhs.language)
      > | && (lhs.caption == rhs.caption)
      > | )
      > | {
      > | return true;
      > | }
      > |
      > | return false;
      > | }
      > |
      > | public static bool operator !=(Resource lhs, Resource rhs)
      > | {
      > | return !(lhs == rhs);
      > | }
      > |
      > | public override bool Equals(object obj)
      > | {
      > | if (! (obj is Resource))
      > | return false;
      > |
      > | return this == (Resource)obj;
      > | }
      > |
      > | }
      >
      >
      > - --
      > Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
      > ray underscore usenet at yahoo dot com
      > -----BEGIN PGP SIGNATURE-----
      > Version: GnuPG v1.2.3 (MingW32)
      > Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
      >
      > iD8DBQE/oVNWwEwccQ4rWPg RAuW3AJ98MVeZtV 9DQwqWRLmqt0V/4jpvOACeMAUo
      > 4LBH2gqFoHd9tK5 WmeDhcwE=
      > =UPTy
      > -----END PGP SIGNATURE-----
      >[/color]


      Comment

      • Guinness Mann

        #4
        Re: Object.GetHashC ode()

        In article <OzPmcAxnDHA.20 64@TK2MSFTNGP11 .phx.gbl>,
        mvp@spam.guard. caspershouse.co m says...[color=blue]
        > Ray and Guiness Mann,
        > When overriding GetHashCode, you can just get the hashcode of the three
        > items you are comparing, and XOR all of them together, and that should give
        > you a hashcode that you can return.[/color]

        Ray and Nicholas,

        Let's take a step back. Did I need to override object.Equals, anyway,
        or can I just provide my own Resource.Equals ?

        -- Rick

        Comment

        • Ray Hsieh (Ray Djajadinata)

          #5
          Re: Object.GetHashC ode()

          -----BEGIN PGP SIGNED MESSAGE-----
          Hash: SHA1

          Rick, you need to override object.Equals() , *in* your Resource class.

          Also, Nicholas, for the HashCode may I suggest this generic formula:

          int result = 17;
          result = 37*result + rId.GetHashCode ();
          result = 37*result + language.GetHas hCode();
          result = 37*result + caption.GetHash Code();
          hashCode = result;

          (17 and 37 are arbitrary--37 is chosen because it is prime.)

          This way, the order of your field is taken into account. Consider this:

          ~ Resource res1 = new Resource(12, "en", "blah");
          ~ Resource res2 = new Resource(12, "blah", "en");

          They are not equal, but if you XOR the fields to get the HashCode, they
          will have identical hashcode. Granted, the example is contrived for this
          particular Resource class, but you will encounter cases like this in
          other classes, where XOR-ing the hashcodes of the fields is not sufficient.


          Guinness Mann wrote:
          | In article <OzPmcAxnDHA.20 64@TK2MSFTNGP11 .phx.gbl>,
          | mvp@spam.guard. caspershouse.co m says...
          | -- Rick


          - --
          Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
          ray underscore usenet at yahoo dot com
          -----BEGIN PGP SIGNATURE-----
          Version: GnuPG v1.2.3 (MingW32)
          Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

          iD8DBQE/obbewEwccQ4rWPg RAjqoAJ0WQo71Xz kuHTm6CT+nByWGW wzLPACfb49+
          /WbS5gbPdMissPWs Oe/N168=
          =AvxA
          -----END PGP SIGNATURE-----

          Comment

          Working...