-6

I can show a hash code like this :

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

This is very simple , Message box return hash code of "Hello World !" . But I want to know when I use a code Like this :

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

What will happen in this code ?! It give me a code like this "64923656" . if I run my application again it give me another code !!! Is it a random hash code ?! or this is a special word hash code ?!

Thank u for read .

Sinaw
  • 61
  • 2
  • 9

3 Answers3

3

It's calling GetHashCode() on this, which is likely a Windows Form based on your use of MessageBox.

Jesse C. Slicer
  • 19,901
  • 3
  • 68
  • 87
1

It is the HashCode current instance of your class. You are calling Object.GetHashCode method which you inherit from object.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
0

For a pretty good explanation of what GetHashCode() does see this post:

How is GetHashCode() of C# string implemented?

Of particular interest to you is probably

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

I believe this is what causes the hash value of the same string to change between program executions.

Community
  • 1
  • 1
Brandon
  • 702
  • 7
  • 15