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"?
3 Answers
Just take string.GetHashCode() which returns an int from a string with very low collision probability.
11 Comments
Be wary of string.GetHashCode().
The .Net documentation states https://msdn.microsoft.com/en-us/library/system.string.gethashcode(v=vs.110).aspx
The hash code itself is not guaranteed to be stable. Hash codes for identical strings states can differ across versions of the.NET Framework and across platforms (such as 32-bit and 64-bit) for a single version of the .NET Framework. In some cases, they can even differ by application domain
1 Comment
GetHashCode will generate a different value for each execution of the application. Its easy to test, just write a console app, like Console.WriteLine("".GetHashCode().ToString()); and run the application twice. The output will be different each time.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.