I have a few classes with heterogenous keys - int and string - and i want to work with them through common interface. It's pretty simple just convert int to string but it obviously will cause perfomance issues. Another options I see are box them to "object", which also doesn't seems perfect, or somehow generate unique integers from string (there will be no joins between former "string" and "int" so they must be unique only in "string" domain) and the quetsion here is "how"?
Just take string.GetHashCode()
which returns an int
from a string
with very low collision probability.
As @tudor pointed out GetHashCode is the supported way of producing hash code from strings (and other objects). Unfortunately there is no way to do such transformation so an integer represent unique strings unless you put severe restrictions on set of strings.
I.e. if your strings short enough (i.e. 2 Unicode or 4 ASCII characters) than there is obvious one-to-one mapping, or if your set of strings is limited and known in advance.
Some reading on the subject: the underlying problem called pigeonhole principle which guarantees collision. Due to Birthday paradox collisions are very likely to happen on reasonably small sets.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With