您可以轻松生成此类碰撞(请参见
https://en.wikipedia.org/wiki/Birthday_problem
),例如。
// key - computed hash value
// value - original string
Dictionary<int, string> hashes = new Dictionary<int, string>();
for (int i = 0; ; ++i) {
string st = i.ToString();
int hash = st.GetHashCode();
string collision = null;
if (hashes.TryGetValue(hash, out collision)) {
Console.Write($"Collision: \"{collision}\" and \"{st}\" hash {hash}");
break;
}
else
hashes.Add(hash, st);
}
结果(在我的工作站.Net 4.6.1 x86上):
Collision: "699391" and "1241308" hash -1612916492
Collision: "942" and "9331582" hash -1864841629
因此,如果您想看到键冲突(在x86模式下):
// Both "699391" and "1241308" keys have the same hash -1612916492
Dictionary<string, string> demo = new Dictionary<string, string>() {
{"699391", "abc"},
{"1241308", "def"},
};
最后
String.GetHashCode
是
内部加工
可以依赖