エクスポート (0) 印刷
すべて展開
展開 最小化

String.GetHashCode メソッド

この文字列のハッシュ コードを返します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)

public override int GetHashCode ()
public int GetHashCode ()
public override function GetHashCode () : int
適用できません。

戻り値

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

GetHashCode の動作は実装に依存します。この実装は、共通言語ランタイムのあるバージョンから別のバージョンに変更される可能性があります。GetHashCode のパフォーマンス向上がその理由です。

さまざまな入力文字列を使用する GetHashCode メソッドのコード例を次に示します。

// 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.
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 Server 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

Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。

.NET Framework

サポート対象 : 3.0,2.0,1.1,1.0

.NET Compact Framework

サポート対象 : 2.0,1.0

XNA Framework

サポート対象 : 1.0

コミュニティの追加

追加
表示:
© 2014 Microsoft