Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

It is really important to make sure you keep the override of .GetHashCode() in step with .Equals().

Basically, you must make sure they consider the same fields so as not to violate the first of the three rules of GetHashCode (from MSDN object.GetHashCode())

If two objects compare as equal, the GetHashCode method for each object must return the same value. However, if two objects do not compare as equal, the GetHashCode methods for the two object do not have to return different values.

In other words, you must make sure that every time .Equals considers two instances equal, they will also have the same .GetHashCode().

As mentioned by someone else here, this questionthis question details a good implementation. In case you are interested, I wrote up a few blog articles on investigating hash codes early last year. You can find my ramblings here (the first blog entry I wrote on the subject)

It is really important to make sure you keep the override of .GetHashCode() in step with .Equals().

Basically, you must make sure they consider the same fields so as not to violate the first of the three rules of GetHashCode (from MSDN object.GetHashCode())

If two objects compare as equal, the GetHashCode method for each object must return the same value. However, if two objects do not compare as equal, the GetHashCode methods for the two object do not have to return different values.

In other words, you must make sure that every time .Equals considers two instances equal, they will also have the same .GetHashCode().

As mentioned by someone else here, this question details a good implementation. In case you are interested, I wrote up a few blog articles on investigating hash codes early last year. You can find my ramblings here (the first blog entry I wrote on the subject)

It is really important to make sure you keep the override of .GetHashCode() in step with .Equals().

Basically, you must make sure they consider the same fields so as not to violate the first of the three rules of GetHashCode (from MSDN object.GetHashCode())

If two objects compare as equal, the GetHashCode method for each object must return the same value. However, if two objects do not compare as equal, the GetHashCode methods for the two object do not have to return different values.

In other words, you must make sure that every time .Equals considers two instances equal, they will also have the same .GetHashCode().

As mentioned by someone else here, this question details a good implementation. In case you are interested, I wrote up a few blog articles on investigating hash codes early last year. You can find my ramblings here (the first blog entry I wrote on the subject)

Source Link
Rob Levine
  • 41.5k
  • 13
  • 88
  • 114

It is really important to make sure you keep the override of .GetHashCode() in step with .Equals().

Basically, you must make sure they consider the same fields so as not to violate the first of the three rules of GetHashCode (from MSDN object.GetHashCode())

If two objects compare as equal, the GetHashCode method for each object must return the same value. However, if two objects do not compare as equal, the GetHashCode methods for the two object do not have to return different values.

In other words, you must make sure that every time .Equals considers two instances equal, they will also have the same .GetHashCode().

As mentioned by someone else here, this question details a good implementation. In case you are interested, I wrote up a few blog articles on investigating hash codes early last year. You can find my ramblings here (the first blog entry I wrote on the subject)