关于c#:这个哈希来自什么?

What is this hash come from?

本问题已经有最佳答案,请猛点这里访问。

我可以这样显示哈希代码:

1
2
            string str ="Hello World !";
        MessageBox.Show(str.GetHashCode().ToString());

这很简单,消息框返回哈希码"你好,世界!".但我想知道什么时候我使用这样的代码:

1
            MessageBox.Show(GetHashCode().ToString());

在这个代码中会发生什么?!给我一个这样的代码"64923656"。如果我再次运行我的应用程序,它会给我另一个代码!!!!它是随机散列码吗?!或者这是一个特殊的单词哈希代码?!

谢谢你的阅读。


它在this上调用GetHashCode(),这很可能是基于您使用MessageBox的Windows窗体。


它是您类的HashCode当前实例。您正在调用从对象继承的Object.GetHashCode方法。


有关getHashCode()的详细说明,请参阅以下文章:

C字符串的getHashCode()是如何实现的?

你特别感兴趣的可能是

The s_UseRandomizedStringHashing variable enables a secure version of the hashing algorithm, designed to keep programmers out of trouble that do something unwise like using GetHashCode() to generate hashes for things like passwords or encryption. It is enabled by an entry in the app.exe.config file

我相信这就是导致相同字符串的散列值在程序执行之间发生变化的原因。