Question

I assume that when doing a look-up in a dictionary it needs to hash the key you are giving it and then use that hash to find the object you are looking for.

If this is so, does using larger objects as keys slow down this look-up significantly or have other consequences that would not be encountered by using a string or simple data type as a key?

Était-ce utile?

La solution

Yes, it's a bad idea to use a mutable object as a dictionary key.

Taking a look at https://stackoverflow.com/a/7941876/400760, leads me to believe there will be unintended consequences, even with a correctly implemented GetHashCode() based on how hash based collections are typically implemented.

It should be safe to use an immutable object as a dictionary key.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top