C# String GetHashCode Method Example

In this source code example, we will demonstrate the usage of String.GetHashCode() method in C# with an example.

The C# String.GetHashCode() method is used to get the hash code of this string. It returns an integer value.

C# String GetHashCode Method Example

The following example demonstrates the String.GetHashCode() method:
using System;

class GetHashCode 
{
    public static void Main() 
    {
        DisplayHashCode( "" );
        DisplayHashCode( "a" );
        DisplayHashCode( "ab" );
        DisplayHashCode( "abc" );
        DisplayHashCode( "abd" );
        DisplayHashCode( "abe" );
        DisplayHashCode( "abcdef" );
        DisplayHashCode( "abcdeg" );
        DisplayHashCode( "abcdeh" );
        DisplayHashCode( "abcdei" );
        DisplayHashCode( "Abcdeg" );
        DisplayHashCode( "Abcdeh" );
        DisplayHashCode( "Abcdei" );
    }

    static void DisplayHashCode( String Operand )
    {
        int     HashCode = Operand.GetHashCode( );
        Console.WriteLine("The hash code for \"{0}\" is: 0x{1:X8}, {1}",
                          Operand, HashCode );
    }
}

Output:

The hash code for "" is: 0x162A16FE, 371857150
The hash code for "a" is: 0x162CB7BD, 372029373
The hash code for "ab" is: 0x412F7A47, 1093630535
The hash code for "abc" is: 0x418632AA, 1099313834
The hash code for "abd" is: 0x418632A3, 1099313827
The hash code for "abe" is: 0x418632A4, 1099313828
The hash code for "abcdef" is: 0x9C50C89B, -1672427365
The hash code for "abcdeg" is: 0x3EF83D36, 1056455990
The hash code for "abcdeh" is: 0xCC640CF9, -865858311
The hash code for "abcdei" is: 0x6F0B8194, 1863025044
The hash code for "Abcdeg" is: 0x3EF8BC56, 1056488534
The hash code for "Abcdeh" is: 0xCC648C19, -865825767
The hash code for "Abcdei" is: 0x6F0C00B4, 1863057588

Related C# String Examples

References


Comments