Why doesn't the Dictionary class have logic to move an object to the correct bucket when it detects that the object's hashcode has changed?
Just to be sure, we are talking about the hash of the key here, the hash of the value is irrelevant.
The important part of your comment is: [...] when it detects that the object's hashcode has changed. ...but how?
- Continuously iterating over all entries in the dictionary would be both inefficient and not thread safe.
- The other option would that for each possible key, you should add a a listener's mechanism to trigger an event when the key's value/hashcode changes. That would really be a PITA and inefficient too.
Since it's pretty rare that you have to modify keys within your dictionary, it's by far easier and more efficient to remove the entry with the old key and add it back with the new key.