C# |类型。GetHashCode()方法

原文:https://www . geesforgeks . org/c-sharp-type-gethashcode-method/

类型。GetHashCode()方法用于返回这个实例的哈希代码。

语法:public override int GetHashCode();

返回值:该方法返回当前实例的哈希代码。

以下程序说明了类型的使用。GetHashCode() 方法:

例 1:

// C# program to demonstrate the
// Type.GetHashCode() Method
using System;
using System.Globalization;
using System.Reflection;
using System.Collections.Generic;

class GFG {

    // Main Method
    public static void Main()
    {
        // Declaring and initializing object of Type
        Type objType = typeof(Dictionary<, >);

        // Getting hashcode of given type
        // using GetHashCode() Method
        int hash = objType.GetHashCode();

        // Display the Result
        Console.Write("Hashcode is {0}", hash);
    }
}

Output:

Hashcode is 32340152

例 2:

// C# program to demonstrate the
// Type.GetHashCode() Method
using System;
using System.Globalization;

class GFG {

    // Main Method
    public static void Main()
    {
        // calling get() method
        get(typeof(int));
        get(typeof(string));
        get(typeof(decimal));
        get(typeof(float));
    }

    // defining get() method
    public static void get(Type objType)
    {

        // Getting hashcode of given type
        // using GetHashCode() Method
        int hash = objType.GetHashCode();

        // Display the Result
        Console.WriteLine("Hashcode of {0} is {1}", objType, hash);
    }
}

Output:

Hashcode of System.Int32 is 18784216
Hashcode of System.String is 18793840
Hashcode of System.Decimal is 19137544
Hashcode of System.Single is 18792864

参考: