GetHashCode override

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Mittenzwey

    #1

    GetHashCode override

    Ok, I've read in the docs, some books and some articles by prominant
    dotneters about how to override GetHashCode, but it still leaves me somewhat
    puzzled.

    I have a custom object which I want to be able to have sorted correctly in a
    sortedlist/hashtable... For that to work correctly, I need to override
    Equals(). Fair enough, otherwise the sort would not really know what
    represents the value of my object. Then the compiler tells me that I need to
    also override the GetHashCode. At first thought, it seems easy, just pass
    the call on to my describing property ( a stirng) and it would seem to work.

    Ah, but that seems to break the rules for GetHashCode. As I see it, I need
    to guarantee that all objects which represent the same value return the same
    Hash Code (fine so far), and I need to guarantee that once an object has
    supplied a hash code, it will never change. Conflicts start here. What if I
    have two objects which have there properties set to two different values,
    and so have reported back two different hash values. Then a program changes
    the property of one of the objects to be the same as the other. Now I have
    two objects which represent the same value, but with two different hash
    codes. I seem to be stuck in a catch-22 here. I have to have all objects
    representing the same value return the same hash, but I can't change the
    hash to make a changed object compliant. What if the objects I wanted to
    sort were based off of a property like priority. It is reasonable to have
    multiple objects with the same priority in a list, and I would want the
    objects to be sorted in the list based off the priority. Also it is common
    for a priority to change, in which case, I would want that object to move to
    a different location in the list.

    Can anyone explain this? Am I just misusing the lists?


  • Jon Skeet [C# MVP]

    #2
    Re: GetHashCode override

    Bill Mittenzwey <bmittenzwey@ho tmail.com> wrote:[color=blue]
    > Ok, I've read in the docs, some books and some articles by prominant
    > dotneters about how to override GetHashCode, but it still leaves me somewhat
    > puzzled.
    >
    > I have a custom object which I want to be able to have sorted correctly in a
    > sortedlist/hashtable... For that to work correctly, I need to override
    > Equals(). Fair enough, otherwise the sort would not really know what
    > represents the value of my object. Then the compiler tells me that I need to
    > also override the GetHashCode. At first thought, it seems easy, just pass
    > the call on to my describing property ( a stirng) and it would seem to work.[/color]

    Yup.
    [color=blue]
    > Ah, but that seems to break the rules for GetHashCode. As I see it, I need
    > to guarantee that all objects which represent the same value return the same
    > Hash Code (fine so far), and I need to guarantee that once an object has
    > supplied a hash code, it will never change. Conflicts start here. What if I
    > have two objects which have there properties set to two different values,
    > and so have reported back two different hash values. Then a program changes
    > the property of one of the objects to be the same as the other. Now I have
    > two objects which represent the same value, but with two different hash
    > codes.[/color]

    As soon as types are mutable, you've got a problem whenever they're
    stored in hash tables, sorted lists or whatever. Don't forget that
    Equals will suddenly change as well - something that *was* Equal to
    another thing possibly won't be any more.

    Do you *definitely* need your property to be settable?

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Bill Mittenzwey

      #3
      Re: GetHashCode override

      After some thought, I would consider making that particular class a one-time
      write class, but what of the situation I mentioned in the first message,
      what you want to sort on is a priority of importance of tasks or something
      where the priority is subject to change?

      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
      news:MPG.19ebb9 536b8347199897f d@msnews.micros oft.com...[color=blue]
      > Bill Mittenzwey <bmittenzwey@ho tmail.com> wrote:[color=green]
      > > Ok, I've read in the docs, some books and some articles by prominant
      > > dotneters about how to override GetHashCode, but it still leaves me[/color][/color]
      somewhat[color=blue][color=green]
      > > puzzled.
      > >
      > > I have a custom object which I want to be able to have sorted correctly[/color][/color]
      in a[color=blue][color=green]
      > > sortedlist/hashtable... For that to work correctly, I need to override
      > > Equals(). Fair enough, otherwise the sort would not really know what
      > > represents the value of my object. Then the compiler tells me that I[/color][/color]
      need to[color=blue][color=green]
      > > also override the GetHashCode. At first thought, it seems easy, just[/color][/color]
      pass[color=blue][color=green]
      > > the call on to my describing property ( a stirng) and it would seem to[/color][/color]
      work.[color=blue]
      >
      > Yup.
      >[color=green]
      > > Ah, but that seems to break the rules for GetHashCode. As I see it, I[/color][/color]
      need[color=blue][color=green]
      > > to guarantee that all objects which represent the same value return the[/color][/color]
      same[color=blue][color=green]
      > > Hash Code (fine so far), and I need to guarantee that once an object has
      > > supplied a hash code, it will never change. Conflicts start here. What[/color][/color]
      if I[color=blue][color=green]
      > > have two objects which have there properties set to two different[/color][/color]
      values,[color=blue][color=green]
      > > and so have reported back two different hash values. Then a program[/color][/color]
      changes[color=blue][color=green]
      > > the property of one of the objects to be the same as the other. Now I[/color][/color]
      have[color=blue][color=green]
      > > two objects which represent the same value, but with two different hash
      > > codes.[/color]
      >
      > As soon as types are mutable, you've got a problem whenever they're
      > stored in hash tables, sorted lists or whatever. Don't forget that
      > Equals will suddenly change as well - something that *was* Equal to
      > another thing possibly won't be any more.
      >
      > Do you *definitely* need your property to be settable?
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: GetHashCode override

        Bill Mittenzwey <bmittenzwey@ho tmail.com> wrote:[color=blue]
        > After some thought, I would consider making that particular class a one-time
        > write class[/color]

        Right - in which case, it would be best to put that one-time-write in
        the constructor, or *very carefully* document that it should be set
        *before* putting it in a hash table etc.
        [color=blue]
        > but what of the situation I mentioned in the first message,
        > what you want to sort on is a priority of importance of tasks or something
        > where the priority is subject to change?[/color]

        Then I wouldn't make the priority part of the equality contract, I
        suspect. I very rarely override Equals.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Guinness Mann

          #5
          Re: GetHashCode override

          In article <eb0$FqDjDHA.27 28@TK2MSFTNGP10 .phx.gbl>,
          bmittenzwey@hot mail.com says...[color=blue]
          > ...but what of the situation I mentioned in the first message,
          > what you want to sort on is a priority of importance of tasks
          > or something where the priority is subject to change?[/color]

          Then a hash table is probably not your best container, but be that as it
          may, why not pull the item out of the list, change the priority and re-
          insert it?

          -- Rick

          Comment

          Working...