C# String - GetHashCode() Method



The C# String GetHashCode() method is used to determine a has code for a string. A hash code is a numerical value that represents the content of an object, such as a string and is often used in hashing algorithms and data structures like hash tables.

Syntax

Following is the syntax of the C# string GetHashCode() method −

public override int GetHashCode ();

Parameters

This method does not accepts any parameters.

Return value

This method returns a 32-bit signed integer has code.

Example 1: Display hashCode of the string.

Following is the basic example of theGetHashCode()method to get hash code of all the specified string −

    
using System;
class Program {
   static void Main() {
      string str1 = "Hello";
      string str2 = "tutotisldpoint";
      string str3 = "India";

      Console.WriteLine($"Hash code for '{str1}': {str1.GetHashCode()}");
      Console.WriteLine($"Hash code for '{str2}': {str2.GetHashCode()}");
      Console.WriteLine($"Hash code for '{str3}': {str3.GetHashCode()}");
   }
}

Output

Following is the output −

Hash code for 'Hello': -498165893
Hash code for 'tutotisldpoint': 1462438067
Hash code for 'India': -1602197555

Example 2: Using GetHashCode in a Dictionary

Let us look at another example of the GetHashCode() method to get the hash code of the dictionary's elements −

using System;
using System.Collections.Generic;
class Program {
   static void Main() {
      Dictionary<int, string> hashDict = new Dictionary<int, string>();

      string[] fruits = { "apple", "guava", "cherry", "banana" };
      foreach (string word in fruits) {
         int hash = word.GetHashCode();
         hashDict[hash] = word;
         Console.WriteLine($"Word: {word}, Hash Code: {hash}");
      }
      int hashToFind = "banana".GetHashCode();
      Console.WriteLine($"Value for hash code {hashToFind}: {hashDict[hashToFind]}");
   }
}

Output

Following is the output −

The number of vowels in the string is: 5

Example 3: Comparing Hash Code of a String

In this example, we use the GetHashCode() method to get the hashcode of the specified string and compare the hash codes of each string −

using System;
class Program {
   static void Main() {
      string str1 = "tutorialspoint";
      string str2 = "India";
      string str3 = "tutorialspoint";

      // Get hash codes for the strings
      int hash1 = str1.GetHashCode();
      int hash2 = str2.GetHashCode();
      int hash3 = str3.GetHashCode();

      Console.WriteLine($"Hash code for '{str1}': {hash1}");
      Console.WriteLine($"Hash code for '{str2}': {hash2}");
      Console.WriteLine($"Hash code for '{str3}': {hash3}");

      // Check if hash codes match
      Console.WriteLine($"Hash code of '{str1}' and '{str2}' are equal: {hash1 == hash2}");
      Console.WriteLine($"Hash code of '{str1}' and '{str3}' are equal: {hash1 == hash3}");
   }
}

Output

Following is the output −

Hash code for 'tutorialspoint': -246238028
Hash code for 'India': -1602197555
Hash code for 'tutorialspoint': -246238028
Hash code of 'tutorialspoint' and 'India' are equal: False
Hash code of 'tutorialspoint' and 'tutorialspoint' are equal: True
csharp_strings.htm
Advertisements