tags:

views:

8418

answers:

8

I read in the Essential C# 3.0 and .NET 3.5 book that:

"GetHashCode()’s returns over the life of a particular object should be constant (the same value), even if the object’s data changes. In many cases, you should cache the method return to enforce this."

Is this a valid guideline?

I have tried a couple built-in types in .NET and they didn't behave like this.

+5  A: 

From MSDN

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.

The GetHashCode method for an object must consistently return the same hash code as long as there is no modification to the object state that determines the return value of the object's Equals method. Note that this is true only for the current execution of an application, and that a different hash code can be returned if the application is run again.

For the best performance, a hash function must generate a random distribution for all input.

This means that if the value(s) of the object change, the hash code should change. For example, a "Person" class with the "Name" property set to "Tom" should have one hash code, and a different code if you change the name to "Jerry". Otherwise, Tom == Jerry, which is probably not what you would have intended.


Edit:

Also from MSDN:

Derived classes that override GetHashCode must also override Equals to guarantee that two objects considered equal have the same hash code; otherwise, the Hashtable type might not work correctly.

From MSDN's hashtable entry:

Key objects must be immutable as long as they are used as keys in the Hashtable.

The way I read this is that mutable objects should return different hashcodes as their values change, unless they are designed for use in a hashtable.

In the example of System.Drawing.Point, the object is mutable, and does return a different hashcode when the X or Y value changes. This would make it a poor candidate to be used as-is in a hashtable.

Jon B
GetHashCode() is designed for use in a hashtable, that's the sole point of this function.
skolima
@skolima - the MSDN documentation is inconsistent with that. Mutable objects may implement GetHashCode(), and should return different values as the object's value changes. Hashtables must use immutable keys. Hence, you can use GetHashCode() for something other than a hashtable.
Jon B
+3  A: 

Not directly answering your question, but - if you use Resharper, do not forget it has a feature that generates a reasonable GetHashCode implementation (as well as the Equals method) for you. You can of course specify which members of the class will be taken into account when computing the hashcode.

petr k.
Thanks, actually I never used Resharper but I keep seeing it mentioned quite often, so I should give it a try.
Joan Venge
+21  A: 

The answer is yes, it is a valid guideline.

The point being made is that for mutable types, you cannot base the hash code on the mutable data because two equal objects must return the same hash code and the hash code has to be valid for the lifetime of the object. If the hash code changes, you end up with an object that gets lost in a hashed collection because it no longer lives in the correct hash bin.

For example, object A returns hash of 1. So, it goes in bin 1 of the hash table. Then you change object A such that it returns a hash of 2. When a hash table goes looking for it, it looks in bin 2 and can't find it - the object is orphaned in bin 1. This is why the hash code must not change for the lifetime of the object, and just one reason why writing GetHashCode implementations is a pain in the butt.

Jeff Yates
How do you determine equality between mutable types?
Jon B
You shouldn't be using GetHashCode to determine equality.
JSBangs
@JS Bangs - From MSDN: Derived classes that override GetHashCode must also override Equals to guarantee that two objects considered equal have the same hash code; otherwise, the Hashtable type might not work correctly.
Jon B
Thanks. Interesting point, but when I try it on say the Point type, changing X, or Y returns a different hashcode than before it's changed. Why did the .NET team ignored this, because Point has mutable data.
Joan Venge
@Joan Venge: Two things. First, not even Microsoft has got GetHashCode right at every implementation. Second, value types are generally immutable with every value being a new instance rather than a modification of an existing instance.
Jeff Yates
System.Drawing.Point is not immutable. You can change the X and Y properties, and doing so results in a different hash code.
Jon B
Thanks Jeff it makes sense. So say for Point type, when I change X, it creates a new Point value, and assign to itself?
Joan Venge
No, Point is unfortunately, mutable and breaks the rules of GetHashCode, as Jon B just pointed out. Like I said, not even Microsoft got this one quite right.
Jeff Yates
Thanks guys. Then it's safe to assume there are probably more types that have bad GetHashCode implementations like this one, right? Do you know any type like Point type that's mutable but has the correct GetHashCode implementation?
Joan Venge
It's only a major issue if you intend to use the type in situations that require a valid hash (such as in a HashTable). if that's the case, the easiest way might be to wrap the Point type in an immutable variant of your own that somehow achieves the three goals of GetHashCode.
Jeff Yates
Thanks. What about the types like Point type that's mutable but has the correct GetHashCode implementation? I just wanna inspect those?
Joan Venge
Point has an incorrect GetHashCode if it is mutable and GetHashCode returns a different value when an instance is mutated.
Jeff Yates
Sorry I meant to say, types unlike Point where they have correct GetHashCode implementation. Do you know any of those? But it should still have mutable members so I can see how they dealt with it.
Joan Venge
Well, the framework does this for reference types by just using a counter, I believe, that increments by one for each object. But this doesn't follow the random distribution rule. You could generate a random number, but I don't know if this is advisable. Anyone know?
Jeff Yates
If you generate a random number, then objects with equal values will not have identical hash codes.
Jon B
How about generating a random number but for Equals comparison use the actual values, like X and Y? Would it work?
Joan Venge
Ah, well, that's where the difficult bit comes in.
Jeff Yates
Good one. Is there a good sample at least I can kinda use as a base? Maybe there is a type that's implemented like this so you can at least imitate the same, which would be very good.Either way, the idea I suggested is doable?
Joan Venge
If you want a mutable type that can be used in a HashTable, you need to freeze the mutation while the object is in the hashtable unless the hash is not tied to the mutable properties. However, if the mutable properties are involved in equality, then they must affect the hash too. However...
Jeff Yates
...remember that while two instances that equal must have the same hash, not all instances with the same hash are equal.
Jeff Yates
How do I implement equality on a mutable reference-type object? please view http://stackoverflow.com/questions/2626547/problem-with-custom-equality-in-entity-framework
Shimmy
@Shimmy: You should undelete your question and answer; they could be useful to another programmer in future who faces the same problem and wants a solution like the one you found.
Jeff Yates
I am sorry, it was a mistake, I moved the question: http://stackoverflow.com/questions/2626839/how-to-implement-iequatablet-when-mutable-fields-are-part-of-the-equality-pro
Shimmy
+5  A: 

This is good advice. Here's what Brian Pepin has to say on the matter:

This has tripped me up more than once: Make sure GetHashCode always returns the same value across the lifetime of an instance. Remember that hash codes are used to identify "buckets" in most hashtable implementations. If an object's "bucket" changes, a hashtable may not be able to find your object. These can be very hard bugs to find, so get it right the first time.

fatcat1111
Why the two downvotes? Is this incorrect? If so, please post a comment explaining how.
fatcat1111
+3  A: 

The hashcode never changes, but it's also important to understand where the Hashcode is coming from.

If your object is using value semantics, i.e. the object's identity is defined by its values (like String, Color, all structs). If your object's identity is independent of all of its values, then the Hashcode is identified by a subset of its values. For example, your StackOverflow entry is stored in a database somewhere. If you change your name or email, your customer entry stays the same, although some values have changed (ultimately you're usually identified by some long customer id #).

So in short:

Value type semantics - Hashcode is defined by values Reference type semantics - Hashcode is defined by some id

I suggest you read Domain Driven Design by Eric Evans, where he goes into entities vs value types (which is more or less what I attempted to do above) if this still doesn't make sense.

DavidN
This is not really correct. The hash code must remain constant for a particular instance. In the case of value types, it is often the case that each value is a unique instance and therefore, the hash appears to change, but actually its a new instance.
Jeff Yates
You're right, value types are immutable so they preclude changing. Good catch.
DavidN
+8  A: 

I think that the documentation regarding GetHashcode is a bit confusing.

On one hand, MSDN states that the hashcode of an object should never change , and be constant On the other hand, MSDN also states that the return value of GetHashcode should be equal for 2 objects, if those 2 objects are considered to be equal.

MSDN:

A hash function must have the following properties:

  • 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.
  • The GetHashCode method for an object must consistently return the same hash code as long as there is no modification to the object state that determines the return value of the object's Equals method. Note that this is true only for the current execution of an application, and that a different hash code can be returned if the application is run again.
  • For the best performance, a hash function must generate a random distribution for all input.

Then, this means that all your objects should be immutable, or the GetHashcode method should be based on properties of your object that are immutable. Suppose for instance that you have this class (naive implementation):

public class SomeThing
{
      public string Name {get; set;}

      public override GetHashCode()
      {
          return Name.GetHashcode();
      }

      public override Equals(object other)
      {
           SomeThing = other as Something;
           if( other == null ) return false;
           return this.Name == other.Name;
      }
}

This implementation already violates the rules that can be found in MSDN. Suppose you have 2 instances of this class; the Name property of instance1 is set to 'Pol', and the Name property of instance2 is set to 'Piet'. Both instances return a different hashcode, and they're also not equal. Now, suppose that I change the Name of instance2 to 'Pol', then, according to my Equals method, both instances should be equal, and according to one of the rules of MSDN, they should return the same hashcode.
However, this cannot be done, since the hashcode of instance2 will change, and MSDN states that this is not allowed.

Then, if you have an entity, you could maybe implement the hashcode so that it uses the 'primary identifier' of that entity, which is maybe ideally a surrogate key, or an immutable property. If you have a value object, you can implement the Hashcode so that it uses the 'properties' of that value object. Those properties make up the 'definition' of the value object. This is of course the nature of a value object; you're not interested in it's identity, but rather in it's value.
And, therefore, value objects should be immutable. (Just like they are in the .NET framework, string, Date, etc... are all immutable objects).

Another thing that comes in mind:
During which 'session' (I don't know really how I should call this) should 'GetHashCode' return a constant value. Suppose you open up your application, load an instance of an object out of the DB (an entity), and get its hashcode. It will return a certain number. Close the application, and load the same entity. Is it required that the hashcode this time has the same value as when you loaded the entity the first time ? IMHO, not.

Frederik Gheysels
Your example is why Jeff Yates says you cannot base the hash code on the mutable data. You can't stick a mutable object in a Dictionary and expect it to work well if the hash code is based on the mutable values of that object.
Ogre Psalm33
I am not able to see where is the MSDN rule violated? The rule clearly says: The GetHashCode method for an object must consistently return the same hash code as long as there is **no modification to the object state that determines the return value of the object's Equals method**. This means that hashcode of instance2 is allowed to be changed when you change the Name of instance2 to Pol
chikak
-1, for the reason chikak states
David Schmitt
+3  A: 

Check out this blog post from Marc Brooks:

VTOs, RTOs and GetHashCode() -- oh, my!

And then check out the follow up post (can't link as I'm new, but there's a link in the initlal article) which discusses further and covers some minor weaknesses in the initial implementation.

This was everything I needed to know about creating a GetHashCode() implementation, he even provides a download of his method along with some other utilities, in short gold.

Shaun
Thanks! Glad it was helpful.
IDisposable
+7  A: 

It's been a long time, but nevertheless I think it is still necessary to give a correct answer to this question, including explanations about the whys and hows. The best answer so far is the one citing the MSDN exhaustivly - don't try to make your own rules, the MS guys knew what they were doing.

But first things first: The Guideline as cited in the question is wrong.

Now the whys - there are two of them

First why: If the hashcode is computed in a way, that it does not change during the lifetime of an object, even if the object itself changes, than it would break the equals-contract.

Remember: "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."

The second sentence often is misinterpreted as "The only rule is, that at object creation time, the hashcode of equal objects must be equal". Don't really know why, but that's about the essence of most answers here as well.

Think of two objects containing a name, where the name is used in the equals method: Same name -> same thing. Create Instance A: Name = Joe Create Instance B: Name = Peter

Hashcode A and Hashcode B will most likely not be the same. What would now happen, when the Name of instance B is changed to Joe?

According to the guideline from the question, the hashcode of B would not change. The result of this would be: A.Equals(B) ==> true But at the same time: A.GetHashCode() == B.GetHashCode() ==> false.

But exactly this behaviour is forbidden explicitly by the equals&hashcode-contract.

Second why: While it is - of course - true, that changes in the hashcode could break hashed lists and other objects using the hashcode, the reverse is true as well. Not changing the hashcode will in the worst case get hashed lists, where all of a lot of different objects will have the same hashcode and therefor be in the same hash bin - happens when objects are initialized with a standard value, for example.


Now coming to the hows Well, on first glance, there seems to be a contradiction - either way, code will break. But neither problem does come from changed or unchanged hashcode.

The source of the problems is well described in the MSDN:

From MSDN's hashtable entry:

Key objects must be immutable as long as they are used as keys in the Hashtable.

This does mean:

Any object that creates a hashvalue should change the hashvalue, when the object changes, but it must not - absolutely must not - allow any changes to itself, when it is used inside a Hashtable (or any other Hash-using object, of course).

First how Easiest way would of course be to design immutable objects only for the use in hashtables, that will be created as copys of the normal, the mutable objects when needed. Inside the immutable objects, it's obviusly ok to cache the hashcode, since it's immutable.

Second how Or give the object a "you are hashed now"-flag, make sure all object data is private, check the flag in all functions that can change objects data and throw an exception data if change is not allowed (i.e. flag is set). Now, when you put the object in any hashed area, make sure to set the flag, and - as well - unset the flag, when it is no longer needed. For ease of use, I'd advise to set the flag automatically inside the "GetHashCode" method - this way it can't be forgotten. And the explicit call of a "ResetHashFlag" method will make sure, that the programmer will have to think, wether it is or is not allowed to change the objects data by now.

Ok, what should be said as well: There are cases, where it is possible to have objects with mutable data, where the hashcode is nevertheless unchanged, when the objects data is changed, without violating the equals&hashcode-contract.

This does however require, that the equals-method is not based on the mutable data as well. So, if I write an object, and create a GetHashCode method that does calculate a value only once and stores it inside the object to return it on later calls, then I must, again: absolutely must, create a Equals method, that will use stored values for the comparison, so that A.Equals(B) will never change from false to true as well. Otherwise, the contract would be broken. The result of this will usually be that the Equals method doesn't make any sense - it's not the original reference equals, but it is neither a value equals as well. Sometimes, this may be intended behaviour (i.e. customer records), but usually it is not.

So, just make GetHashCode result change, when the object data changes, and if the use of the object inside of hash using lists or objects is intended (or just possible) then make the object either immutable or create a readonly flag to use for the lifetime of a hashed list containing the object.

(By the way: All of this is not C# oder .NET specific - it is in the nature of all hashtable implementations, or more generally of any indexed list, that identifying data of objects should never change, while the object is in the list. Unexpected and unpredictable behaviour will occur, if this rule is broken. Somewhere, there may be list implementations, that do monitor all elements inside the list and do automatic reindexing the list - but the performance of those will surely be gruesome at best.)

Alex
+1 for this detailed explanation (would give more if i could)
Oliver
Yeah don't worry, we are reading :O
Joan Venge