當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C# Type.GetHashCode()用法及代碼示例


Type.GetHashCode()方法用於返回此實例的哈希碼。

用法: public override int GetHashCode ();

返回值:此方法返回當前實例的哈希碼。


以下示例程序旨在說明Type.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); 
    } 
}
輸出:
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); 
    } 
}
輸出:
Hashcode of System.Int32 is 18784216
Hashcode of System.String is 18793840
Hashcode of System.Decimal is 19137544
Hashcode of System.Single is 18792864

參考:



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Type.GetHashCode() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。