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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top