
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
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