다음을 통해 공유


String.GetHashCode 메서드

해당 문자열에 대한 해시 코드를 반환합니다.

네임스페이스: System
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public Overrides Function GetHashCode As Integer
‘사용 방법
Dim instance As String
Dim returnValue As Integer

returnValue = instance.GetHashCode
public override int GetHashCode ()
public:
virtual int GetHashCode () override
public int GetHashCode ()
public override function GetHashCode () : int

반환 값

32비트 부호 있는 정수 해시 코드입니다.

설명

GetHashCode의 동작은 공용 언어 런타임 버전마다 변경될 수 있는 구현에 따라 달라집니다. 이런 일이 일어날 수 있는 이유는 GetHashCode의 성능 개선 때문입니다. GetHashCode의 동작을 계속해서 유지하려면 GetHashCode를 런타임에서 구현할 때 사용자 고유의 구현이 변경되지 않도록 재정의합니다.

예제

다음 코드 예제에서는 다양한 입력 문자열을 사용하여 GetHashCode 메서드를 보여 줍니다.

' Example for the String.GetHashCode( ) method.
Imports System
Imports Microsoft.VisualBasic

Module GetHashCode
   
    Sub Main()
        Console.WriteLine( _
            "This example of String.GetHashCode( ) " & _
            "generates the following output." & vbCrLf)

        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")
    End Sub 'Main
       
    Sub DisplayHashCode(Operand As String)

        Dim HashCode As Integer = Operand.GetHashCode()
        Console.WriteLine( _
            "The hash code for ""{0}"" is: 0x{1:X8}, {1}", _
            Operand, HashCode)

    End Sub 'DisplayHashCode
End Module 'GetHashCode

' This example of String.GetHashCode( ) generates the following output.
' 
' The hash code for "" is: 0x00001505, 5381
' The hash code for "a" is: 0x0002B5C4, 177604
' The hash code for "ab" is: 0x00596E26, 5860902
' The hash code for "abc" is: 0x0B873285, 193409669
' The hash code for "abd" is: 0x0B873282, 193409666
' The hash code for "abe" is: 0x0B873283, 193409667
' The hash code for "abcdef" is: 0x4DDB4BE2, 1306217442
' The hash code for "abcdeg" is: 0x4DDB4BE3, 1306217443
' The hash code for "abcdeh" is: 0x4DDB4BEC, 1306217452
' The hash code for "abcdei" is: 0x4DDB4BED, 1306217453
' The hash code for "Abcdeg" is: 0x941C4FC3, -1810083901
' The hash code for "Abcdeh" is: 0x941C4FCC, -1810083892
' The hash code for "Abcdei" is: 0x941C4FCD, -1810083891
// Example for the String.GetHashCode( ) method.
using System;

class GetHashCode 
{
    public static void Main() 
    {
        Console.WriteLine( 
            "This example of String.GetHashCode( ) " +
            "generates the following output.\n" );

        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 );
    }
}

/*
This example of String.GetHashCode( ) generates the following output.

The hash code for "" is: 0x00001505, 5381
The hash code for "a" is: 0x0002B5C4, 177604
The hash code for "ab" is: 0x00596E26, 5860902
The hash code for "abc" is: 0x0B873285, 193409669
The hash code for "abd" is: 0x0B873282, 193409666
The hash code for "abe" is: 0x0B873283, 193409667
The hash code for "abcdef" is: 0x4DDB4BE2, 1306217442
The hash code for "abcdeg" is: 0x4DDB4BE3, 1306217443
The hash code for "abcdeh" is: 0x4DDB4BEC, 1306217452
The hash code for "abcdei" is: 0x4DDB4BED, 1306217453
The hash code for "Abcdeg" is: 0x941C4FC3, -1810083901
The hash code for "Abcdeh" is: 0x941C4FCC, -1810083892
The hash code for "Abcdei" is: 0x941C4FCD, -1810083891
*/
// Example for the String::GetHashCode( ) method.
using namespace System;
void DisplayHashCode( String^ Operand )
{
   int HashCode = Operand->GetHashCode();
   Console::WriteLine( "The hash code for \"{0}\" is: 0x{1:X8}, {1}", Operand, HashCode );
}

int main()
{
   Console::WriteLine( "This example of String::GetHashCode( ) "
   "generates the following output.\n" );
   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" );
}

/*
This example of String::GetHashCode( ) generates the following output.

The hash code for "" is: 0x00001505, 5381
The hash code for "a" is: 0x0002B5C4, 177604
The hash code for "ab" is: 0x00596E26, 5860902
The hash code for "abc" is: 0x0B873285, 193409669
The hash code for "abd" is: 0x0B873282, 193409666
The hash code for "abe" is: 0x0B873283, 193409667
The hash code for "abcdef" is: 0x4DDB4BE2, 1306217442
The hash code for "abcdeg" is: 0x4DDB4BE3, 1306217443
The hash code for "abcdeh" is: 0x4DDB4BEC, 1306217452
The hash code for "abcdei" is: 0x4DDB4BED, 1306217453
The hash code for "Abcdeg" is: 0x941C4FC3, -1810083901
The hash code for "Abcdeh" is: 0x941C4FCC, -1810083892
The hash code for "Abcdei" is: 0x941C4FCD, -1810083891
*/
// Example for the String.GetHashCode( ) method.
import System.*;

class GetHashCode
{
    public static void main(String[] args)
    {
        Console.WriteLine(("This example of String.GetHashCode( ) " 
            + "generates the following output.\n"));
        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");
    } //main

    static void DisplayHashCode(String operand)
    {
        int hashCode = operand.GetHashCode();
        Console.WriteLine("The hash code for \"{0}\" is: 0x{1}, {2}", 
            operand, ((System.Int32)hashCode).ToString("X8"), 
            ((System.Int32)hashCode).ToString(""));
    } //DisplayHashCode
} //GetHashCode

/*
This example of String.GetHashCode( ) generates the following output.

The hash code for "" is: 0x00001505, 5381
The hash code for "a" is: 0x0002B5C4, 177604
The hash code for "ab" is: 0x00596E26, 5860902
The hash code for "abc" is: 0x0B873285, 193409669
The hash code for "abd" is: 0x0B873282, 193409666
The hash code for "abe" is: 0x0B873283, 193409667
The hash code for "abcdef" is: 0x4DDB4BE2, 1306217442
The hash code for "abcdeg" is: 0x4DDB4BE3, 1306217443
The hash code for "abcdeh" is: 0x4DDB4BEC, 1306217452
The hash code for "abcdei" is: 0x4DDB4BED, 1306217453
The hash code for "Abcdeg" is: 0x941C4FC3, -1810083901
The hash code for "Abcdeh" is: 0x941C4FCC, -1810083892
The hash code for "Abcdei" is: 0x941C4FCD, -1810083891
*/

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

String 클래스
String 멤버
System 네임스페이스
Int32 구조체