次の方法で共有


String.GetHashCode メソッド

このインスタンスのハッシュ コードを返します。

Overrides Public Function GetHashCode() As Integer
[C#]
public override int GetHashCode();
[C++]
public: int GetHashCode();
[JScript]
public override function GetHashCode() : int;

戻り値

32 ビット符号付き整数ハッシュ コード。

解説

GetHashCode の動作は実装に依存します。この実装は、共通言語ランタイムのあるバージョンから別のバージョンに変更される可能性があります。 GetHashCode のパフォーマンス向上がその理由です。 GetHashCode の動作を一定にする必要がある場合は、 GetHashCode のランタイムによる実装を、変更されないことがわかっている独自の実装でオーバーライドしてください。

使用例

[Visual Basic, C#, C++] さまざまな入力文字列を使用する 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

[C#] 
// 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
*/

[C++] 
// Example for the String::GetHashCode( ) method.
#using <mscorlib.dll>
using namespace System;

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

void main() 
{
    Console::WriteLine( 
        S"This example of String::GetHashCode( ) " 
        S"generates the following output.\n" );

    DisplayHashCode( S"" );
    DisplayHashCode( S"a" );
    DisplayHashCode( S"ab" );
    DisplayHashCode( S"abc" );
    DisplayHashCode( S"abd" );
    DisplayHashCode( S"abe" );
    DisplayHashCode( S"abcdef" );
    DisplayHashCode( S"abcdeg" );
    DisplayHashCode( S"abcdeh" );
    DisplayHashCode( S"abcdei" );
    DisplayHashCode( S"Abcdeg" );
    DisplayHashCode( S"Abcdeh" );
    DisplayHashCode( S"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
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

String クラス | String メンバ | System 名前空間 | Int32